Quantcast
Channel: Active questions tagged config - Stack Overflow
Viewing all articles
Browse latest Browse all 5049

Is there a way with python to move files on windows by reading origin and destination directories from a conf file?

$
0
0

I'm trying to automate moving files from one windows directory to another, having the origin and destination directories in a config file.

I've gotten it to work if there is only 1 line in the config file containing the paths.The shutil command also works if I hardcode the paths in the script, but that's not the idea.I need to loop through a list of directories and run the code per origin and destination

This is the entry in the .conf file:

c:\\temp\O D |c:\\temp\D D 

I split the line on the |, and and then I've tried the os.path.join and os.path.normpath but shutil keeps throwing the double backslashes if I add a row to the conf file.

If I add another row, then it throws this error:

FileNotFoundError: [Errno 2] No such file or directory: 'c:\temp\D D \test1.txt'

this is my code so far....

import shutilimport osimport os.pathimport datetimefrom pathlib import Pathfilepath = r'c:\temp\acc.conf.txt'with open(filepath) as fp:   for cnt, line in enumerate(fp):       print("Line {}: {}".format(cnt, line))       a,b = line.split('| ')       source= Path(a)       source1=os.path.join(source)       source_path = os.path.normpath(source1)       #print(source_path)       print(b)       dst= Path(b)       dst1=os.path.join(dst)       dest_path = os.path.normpath(dst1)       #print(dest_path)       os.chdir(source_path)       #print(os.getcwd())       files = os.listdir(os.getcwd())       now = datetime.datetime.now()       run_time= now.strftime("%Y-%m-%d %H:%M:%S")       for f in files:           print(run_time +" - " + f +"- " + os.getcwd() +"- " + dest_path)           #shutil.move(f, dest_path)           print(f)           print(dest_path)           shutil.move(f, dest_path)

Viewing all articles
Browse latest Browse all 5049

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>