1
將文件移入子文件夾我試圖製作一個代碼,將一個目錄中的所有文件組織成子目錄。即2017.2.3,2016.2.5,2015.5.6,2014.3.5放入原始目錄中標記爲2017,2016,2015,2014的文件夾中。使用2.7.13嘗試使用shutil.move()
我正在使用的代碼如下:
import os, shutil
root_path = ('D:\Sigma\Rides')
folders = ['2016', '2017', '2018', '2019', '2020']
for folder in folders:
os.mkdir(os.path.join(root_path,folder))
source = os.listdir('D:\\Sigma\\Rides')
dest1 = ('D:\\Sigma\\Rides\\2016')
dest2 = ('D:\\Sigma\\Rides\\2017')
dest3 = ('D:\\Sigma\\Rides\\2018')
dest4 = ('D:\\Sigma\\Rides\\2019')
dest5 = ('D:\\Sigma\\Rides\\2020')
for files in source:
if (files.startswith('2016_')):
shutil.move(os.path.join(source, files), dest1)
if (files.startswith('2017')):
shutil.move(os.path.join(source, files), dest2)
if (files.startswith('2018')):
shutil.move(os.path.join(source, files), dest3)
if (files.startswith('2019')):
shutil.move(os.path.join(source, files), dest4)
if (files.startswith('2020')):
shutil.move(os.path.join(source, files), dest5)
這是我receieve錯誤:
Traceback (most recent call last):
File "D:\Documents\Programs\Sigma_File_Move.py", line 24, in <module>
shutil.move(os.path.join(source, files), dest1)
File "D:\Python27\ArcGIS10.4\lib\ntpath.py", line 65, in join
result_drive, result_path = splitdrive(path)
File "D:\Python27\ArcGIS10.4\lib\ntpath.py", line 116, in splitdrive
normp = p.replace(altsep, sep)
AttributeError: 'list' object has no attribute 'replace'
任何反饋將不勝感激。
有是一個錯誤。您實際上不能使用'abspath'在這裏創建路徑。我已經改變它在你的代碼中使用'root_path'變量。 – anupsabraham