Right now I have a program which moves files from the sub-directories in the SOURCE folder to sub-directories in the DESTINATION folder. The files contain information like this: content of file before the move.
Now during the move from SOURCE to DESTINATION I want to modify the moving files on 2 places.
- I want to copy Time and paste it as TimeDif under Time. The type stays Y and the value has to be the current time - the value of Time.
- I want to modify the value of Power * 10. If the value of Power =< 1000 then type stays N, if else type of Power = Y
So after the file has been moved from SOURCE to DESTINATION it has to look like this:
content of file after the move.
Here is the code I have right now for moving the files, all the moving works smoothly. I just don't know where to start when I want to modify the content of the file:
import os, os.path
import time
#Make source, destination and archive paths.
source = r'c:\data\AS\Desktop\Source'
destination = r'c:\data\AS\Desktop\Destination'
archive = r'c:\data\AS\Desktop\Archive'
#Make directory paths and make sure to consider only directories under source.
for subdir in os.listdir(source):
subdir_path = os.path.join(source, subdir)
if not os.path.isdir(subdir_path):
continue
#Now we want to get the absolute paths of the files inside those directories
#and store them in a list.
all_file_paths = [os.path.join(subdir_path, file) for file in os.listdir(subdir_path)]
all_file_paths = [p for p in all_file_paths if os.path.isfile(p)]
#Exclude empty sub-directories
if len(all_file_paths) == 0:
continue
#Get only the newest files of those directories.
newest_file_paths = max(all_file_paths, key=os.path.getctime)
#Now we are selecting the files which will be moved
#and make a destination path for them.
for file_path in all_file_paths:
if file_path == newest_file_paths and os.path.getctime(newest_file_paths) < time.time() - 120:
dst_root = destination
else:
dst_root = archive
#Now its time to make the move.
dst_path = os.path.join(dst_root, subdir, os.path.basename(file_path))
os.rename(file_path, dst_path)