Перемещение нескольких файлов в папку в одном каталоге. Ошибка каталога ... Ошибка FileNotFoundError - PullRequest
0 голосов
/ 06 января 2019

Я пытаюсь переместить все файлы в каталоге в папку в том же каталоге, что и файлы.

Вот список файлов:

['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, а затем выдает ошибку исключения. Я пытался переименовать файлы и другие методы shutil, чтобы попытаться обойти ошибку, но ни один не помог.

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'
...