我试图编写一个程序,当用户输入自己的自定义文件目录时重命名文件.
我现在处于非常早期的一部分.这是我第一次使用OS和glob命令.
我的代码如下.但是,当我尝试运行它时,结果是一个空列表.我尝试直接在glob命令中键入一个文件根目录,它以某种方式工作,但结果不是我想要的.
希望你们能帮助我.
谢谢.
import os, glob
def fileDirectory():
#Asks the user for a file root directory
fileroot = raw_input("Please input the file root directory \n\n")
#Returns a list with all the files inside the file root directory
filelist = glob.glob(fileroot)
print filelist
文件目录()
最佳答案 Python是对空白区域敏感的,因此您需要确保函数内部所需的所有内容都是缩进的.
Stackoverflow对代码有自己的缩进要求,这使得很难确定代码最初的缩进.
import os, glob
def fileDirectory():
#Asks the user for a file root directory
fileroot = raw_input("Please input the file root directory \n\n")
#Returns a list with all the files inside the file root directory
filelist = glob.glob(fileroot)
print filelist
fileDirectory()
接下来的事情是glob返回一个glob的结果 – 它没有列出一个目录,这似乎是你想要做的.
你想要os.listdir或os.walk,或者你实际上应该要求一个glob表达式而不是一个目录.
最后raw_input可能会给你一些额外的空格,你必须剥离它们.检查fileroot是什么.
您可能希望拆分程序,以便可以单独调查每个功能.