我正在尝试将目录中的所有文件移动到与文件位于同一目录的文件夹中。
这是文件列表:
['410 A Statement of Organization, Form 410 Amendment 2132018 2018 .pdf', '410 A Statement of Organization, Form 410 Amendment 2142018 2018 .pdf', '410 A Statement of Organization, Form 410 Amendment 4102018 2018 .pdf', '410 A Statement of Organization, Form 410 Amendment 422018 2018 .pdf', '410 A Statement of Organization, Form 410 Amendment 5292018 2018 .pdf', '410 A Statement of Organization, Form 410 Amendment 572018 2018 .pdf', '410 A Statement of Organization, Form 410 Amendment 9222017 2018 .pdf', '460 A Recipient Committee Campaign Statement Amendment 7252018 112018 3312018.pdf', '460 Recipient Committee Campaign Statement 1312018 1012017 12312017.pdf', '460 Recipient Committee Campaign Statement 4302018 112018 3312018.pdf', '460 Recipient Committee Campaign Statement 7312018 412018 6302018.pdf', '497 24 hour Contribution Report 522018 2018.pdf', 'General 1162018 Santa Clara Residents for Responsible Development Issues PAC Build a Better San Jose Support Ballot Measure', 'transactionExportGrid (1).xls', 'transactionExportGrid (2).xls', 'transactionExportGrid (3).xls', 'transactionExportGrid (4).xls', 'transactionExportGrid.xls']
我使用了shutil.move(),它适用于所有410个文件,然后引发异常错误。我尝试重命名文件和其他常规技术来尝试避免该错误,但是没有一个起作用。
import shutil
import os,sys
from os.path import isfile,join
path = os.getcwd()
folders = os.listdir(os.getcwd())
data_path = folders[1]
if data_path == "data":
abs_file_path = os.path.join(path,data_path)
os.chdir(abs_file_path)
new_path = abs_file_path
add_new_folder = new_path + "\\" + outer_form_text[0].replace("/","")
if not os.path.exists(add_new_folder):
os.makedirs(add_new_folder)
root_src_dir = new_path
root_dst_dir = add_new_folder
for src_dir, dirs, files in os.walk(root_src_dir):
dst_dir = src_dir.replace(root_src_dir, root_dst_dir, 1)
if not os.path.exists(dst_dir):
os.makedirs(dst_dir)
for file_ in files:
src_file = os.path.join(src_dir, file_)
dst_file = os.path.join(dst_dir, file_)
if os.path.exists(dst_file):
# in case of the src and dst are the same file
if os.path.samefile(src_file, dst_file):
continue
os.remove(dst_file)
shutil.move(src_file, dst_dir)
FileNotFoundError Traceback (most recent call last)
~\Anaconda3\lib\shutil.py in move(src, dst, copy_function)
556 try:
--> 557 os.rename(src, real_dst)
558 except OSError:
FileNotFoundError: [WinError 3] The system cannot find the path specified: 'C:\\Users\\gille\\Documents\\SJOP\\SJ Webscraping\\data\\460 A Recipient Committee Campaign Statement Amendment 7252018 112018 3312018.pdf' -> 'C:\\Users\\gille\\Documents\\SJOP\\SJ Webscraping\\data\\General 1162018 Santa Clara Residents for Responsible Development Issues PAC Build a Better San Jose Support Ballot Measure\\460 A Recipient Committee Campaign Statement Amendment 7252018 112018 3312018.pdf'
During handling of the above exception, another exception occurred:
FileNotFoundError Traceback (most recent call last)
<ipython-input-64-1dd3a85886a0> in <module>()
16 continue
17 os.remove(dst_file)
---> 18 shutil.move(src_file, dst_dir)
~\Anaconda3\lib\shutil.py in move(src, dst, copy_function)
569 rmtree(src)
570 else:
--> 571 copy_function(src, real_dst)
572 os.unlink(src)
573 return real_dst
~\Anaconda3\lib\shutil.py in copy2(src, dst, follow_symlinks)
255 if os.path.isdir(dst):
256 dst = os.path.join(dst, os.path.basename(src))
--> 257 copyfile(src, dst, follow_symlinks=follow_symlinks)
258 copystat(src, dst, follow_symlinks=follow_symlinks)
259 return dst
~\Anaconda3\lib\shutil.py in copyfile(src, dst, follow_symlinks)
119 else:
120 with open(src, 'rb') as fsrc:
--> 121 with open(dst, 'wb') as fdst:
122 copyfileobj(fsrc, fdst)
123 return dst
FileNotFoundError: [Errno 2] No such file or directory: 'C:\\Users\\gille\\Documents\\SJOP\\SJ Webscraping\\data\\General 1162018 Santa Clara Residents for Responsible Development Issues PAC Build a Better San Jose Support Ballot Measure\\460 A Recipient Committee Campaign Statement Amendment 7252018 112018 3312018.pdf'