Python Forum
copy files from one destination to another by reading filename from csv
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
copy files from one destination to another by reading filename from csv
#3
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
Error:
no file path or directory "filepath"
below is the code
import 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..
Reply


Messages In This Thread
RE: copy files from one destination to another by reading filename from csv - by Prince_Bhatia - Feb-27-2018, 08:15 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Why is the copy method name in python list copy and not `__copy__`? YouHoGeon 2 322 Apr-04-2024, 01:18 AM
Last Post: YouHoGeon
  Copy Paste excel files based on the first letters of the file name Viento 2 501 Feb-07-2024, 12:24 PM
Last Post: Viento
  Create new folders and copy files cocobolli 3 1,547 Mar-22-2023, 10:23 AM
Last Post: Gribouillis
  Copy only hidden files and folders with rsync Cannondale 2 1,072 Mar-04-2023, 02:48 PM
Last Post: Cannondale
  python move specific files from source to destination including duplicates mg24 3 1,142 Jan-21-2023, 04:21 AM
Last Post: deanhystad
  Rename part of filename in multiple files atomxkai 7 7,465 Feb-18-2022, 10:03 PM
Last Post: atomxkai
  Append files and add column with last part of each filename NiKirk 0 2,659 Feb-04-2022, 07:35 AM
Last Post: NiKirk
  Compare filename with folder name and copy matching files into a particular folder shantanu97 2 4,583 Dec-18-2021, 09:32 PM
Last Post: Larz60+
  Reading a copy-protected PDF CaptainCsaba 6 6,297 Oct-25-2021, 07:06 AM
Last Post: Caslenty
  Reading Multiple text Files in pyhton Fatim 1 1,970 Jun-25-2021, 01:37 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020