copy files from one destination to another by reading filename from csv - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: copy files from one destination to another by reading filename from csv (/thread-8586.html) |
copy files from one destination to another by reading filename from csv - Prince_Bhatia - Feb-27-2018 hi, i want to move several file mentioned in csv to another folder path which is mentioned in code i have several pdf's mentioned in the attached csv file, in csv file there is one column with the filename including it's path....but when i try to run this it says below is my code:import shutil import os import csv import sys dataobj = {} filename = "maharera.csv" def main(argv): filename, "D:\maharera", "D:\maharera\copy" = argv[1:] with open(filename, "rb")as csvfile: reader = csv.reader(csvfile) for row in reader: filename = row[0] filepath = row[1] if filename.startswith("D:\maharera"): filename = filename[len(existing_path_prefix):] newfile = os.path.join("D:\maharera\copy", filepath) print ('Copying %s to %s...' % filename, new_filename), shutil.copy(filename, new_filename) if __main__ == "__main__": main(sys.argv) #source = os.listdir("D:\maharera") #print(source) #destination = "D:\maharera\copy" #for files in source: # if files.endswith(".pdf"): # shutil.copyfile(files, destination)can anyone guide me in right direction since i have 15000 pdf out of which in files i have mentioned 501 which needed to move to another folder RE: copy files from one destination to another by reading filename from csv - Larz60+ - Feb-27-2018 Not sure what you're trying to accomplish here: filename, "D:\maharera", "D:\maharera\copy" = argv[1:]did you mean: filename = argv[1]What does argv look like? also, something has changed as line number and code don't match this statement: if __main__ == "__main__":needs to be: if __name__ == '__main__': RE: copy files from one destination to another by reading filename from csv - Prince_Bhatia - Feb-27-2018 ok what i am trying to do is ...writing a module that will read csv , pick file names from there look into the directory where these files resides and then move them to the destination folder...i think i had some error in the above code..i have redone the same code.. but now it says below is the codeimport os import shutil import csv import sys csv_file = "maharera.csv" existing_path_prefix = "D:\maharera" new_path_prefix = "D:\movement" def main(argv): # TODO: this should do some error checking or maybe use optparse csv_file, existing_path_prefix, new_path_prefix = argv[1:] with open(csv_file, 'r') as f: reader = csv.reader(f) for row in reader: # Assuming the column in the CSV file we want is the first one filename = row[0] filepath = row[1] if filename.startswith(existing_path_prefix): filename = filename[len(existing_path_prefix):] new_filename = os.path.join(new_path_prefix, filename) #print ('Copying %s to %s...' % filename, new_filename), #print ('Copying %s to %s...' % filepath, new_filename) #Changed shutil.copy(filepath, new_filename) #Changed print ('done.') #print ('All done!') if __name__ == '__main__': main(sys.argv)i read the code here...https://stackoverflow.com/questions/29850220/read-filenames-from-csv-and-then-copy-the-files-to-different-directory please help me to correct this code so that i can move selected files which are mentioned in csv to new directory...i have attached new cvs file for this code also by editing the original post.. RE: copy files from one destination to another by reading filename from csv - Prince_Bhatia - Feb-27-2018 hi finally,i have found the soultion..this below code will do above mentioned: import os import shutil import csv import sys csv_file = "maharera.csv" existing_path_prefix = "D:\maharera" new_path_prefix = r"D:\new_movement" with open(csv_file, 'r') as f: reader = csv.reader(f) for i, row in enumerate(reader): if i == 0: print(i) pass # Skip header row else: filename, filepath = row new_filename = os.path.join(new_path_prefix, filename) old_filename = os.path.join(filepath, filename) shutil.copy(old_filename, new_filename) |