Marco
Marco

Reputation: 41

How to move a folder of files to a variably named folder

I am trying to move files into a folder which is created and named variably.

import os
import pandas as pd
import shutil
import glob

os.chdir('C:\\profili\\u421562\\Documents\\5- PYTHON DATA\\FOLDER_CREATION_ZONE')

name_of_file = 'Random_NAME'

if os.path.isdir(name_of_KYC) == False
   os.mkdir(name_of_KYC)    
else:
   print("FILE Already EXISTS")

os.mkdir(name_of_KYC+'/Entity Documents')
os.mkdir(name_of_KYC+'/Archive')
os.mkdir(name_of_KYC+'/FinCen adnd IDs')
os.mkdir(name_of_KYC+'/KYC FORMs')

source = 'C:/profili//u421562/Documents//5- PYTHON DATA//KYC_Forms'
dest1 = 'c:/profili/u421562/Documents/5- PYTHON DATA/FOLDER_CREATION_ZONE' + '/' + name_of_file + '/' + 'KYC_FORMs/'

files = os.listdir(source)

for f in files:
    shutil.move(source+f, dest1)

FileNotFoundError:

[Errno 2] No such file or directory:

'C:/profili//u421562/Documents//5- PYTHON DATA//KYC_FormsKYC Checklist_.docx'

Upvotes: 1

Views: 59

Answers (1)

As @packetloss mentioned, the issue with the missing path separator.

To avoid this error in a platform independent manner, use pathlib module. Specifically,

  1. change source = 'C:/profili//u421562/Documents//5- PYTHON DATA//KYC_Forms' to source = pathlib.Path('C:/profili//u421562/Documents//5- PYTHON DATA//KYC_Forms') and
  2. change shutil.move(source+f, dest1) to shutil.move(source / f, dest1).

Upvotes: 1

Related Questions