2017-03-06 50 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")