现在我有一个程序可以将文件从 SOURCE 文件夹中的子目录移动到 DESTINATION 文件夹中的子目录。这些文件包含如下信息: 移动前的文件内容。
现在,在从 SOURCE 到 DESTINATION 的移动过程中,我想在 2 个地方修改移动文件。
我想复制时间并将其粘贴为时间下的 TimeDif。类型保持为 Y,值必须是当前时间 - Time 的值。
我想修改 Power * 10 的值。如果 Power 的值 =< 1000,则类型保持为 N,否则类型为 Power = Y
因此,在文件从 SOURCE 移动到 DESTINATION 后,它必须如下所示:
这是我现在用于移动文件的代码,所有移动工作都很顺利。就是想修改文件内容的时候不知道从何下手:
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
Qyouu
泛舟湖上清波郎朗
慕斯709654
相关分类