2017-03-06 51 views
0

我需要查找文件夹中的所有Excel文件并将它们移动到不同的文件夹。这需要以.py文件的形式运行,而不是从IDLE运行。下面是有问题的部分代码。如何找到并移动文件系统上的文件?

Path = input("Please enter the filepath u wish to search") 
dirs = os.listdir("path") 
for filename in dirs: 
    if filename.endswith(".xlsx"): 
     shutil.move('dirs', "C:\\Folder Sorter\\Excel") 

我进入这个“C:\移动”对输入的我XLSX文件中创建一个文件夹移动。我收到下面的错误。任何帮助将不胜感激。谢谢!

Traceback (most recent call last): 
    File "C:/Toms Stuff/Programing/Python/Git FileSorter/Git File Sorter Final Part 2.py", line 24, in <module> 
    dirs = os.listdir("path") 
WindowsError: [Error 3] The system cannot find the path specified: 'path/*.*' 

回答

0

你的代码看起来不错,但出现一些错误......

1)os.listdir( “路径”)路径存储在变量Path,但你调用listdir同时函数的字符串“路径”

2)shutil移动需要2个参数:源文件和文件夹的结果

Path = input("Please enter the filepath u wish to search") 
dirs = os.listdir(Path) #because "path" is string and not your variable... 
for filename in dirs: 
    if filename.endswith(".xlsx"): 
     shutil.move(Path +"/"+filename, "C:/Folder Sorter/Excel")