0

I'm trying to move all files in a directory to a folder in the same directory as the files.

Here's a list of the files:

['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']

I used shutil.move() and it works for all of the 410 files and then throws an exception error. I've tried renaming files and other shutil techniques to try and circumvent the error, but none have worked.

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'
Glenn G.
  • 419
  • 3
  • 7
  • 18
  • Possible duplicate of [How to use "/" (directory separator) in both Linux and Windows in Python?](https://stackoverflow.com/questions/16010992/how-to-use-directory-separator-in-both-linux-and-windows-in-python) – Ic3fr0g Jan 06 '19 at 04:02
  • Seems pretty complicated for moving files to a directory. – RoadRunner Jan 06 '19 at 04:18
  • No this is different. The problem is not setting up the path to the directory but rather something is going wrong while moving the files to the directory. Some files are moving but others are not, for some reason the shutil.move() is failing at a certain point and I'm struggling to figure out why – Glenn G. Jan 06 '19 at 20:03

0 Answers0