在从一个文件夹到另一个 Python 的传输过程中修改多个文件

现在我有一个程序可以将文件从 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


慕神8447489
浏览 118回答 3
3回答

Qyouu

如果文件很小,那么您可以简单地而不是移动文件:从所有文件中读取信息找到要替换的数据将新数据写入源目录中的文件删除旧文件就像是def move_file(file_path, dst_path):&nbsp; with open(file_path, "r") as input_file, open(dst_path, "w") as output_file:&nbsp; &nbsp; &nbsp; for line in input_file:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if <line meets criteria to modify>:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<modify_line>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;print(line, file=output_file)&nbsp; &nbsp; &nbsp; for <data> in <additional_data>:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;print(<data>, file=output_file)&nbsp; # remove the old file&nbsp; os.remove(file_path)然后代替原始代码中的 os.rename 调用 move_file 函数#Now we are selecting the files which will be moved#and make a destination path for them.&nbsp; &nbsp; for file_path in all_file_paths:&nbsp; &nbsp; &nbsp; &nbsp; if file_path == newest_file_paths and os.path.getctime(newest_file_paths) < time.time() - 120:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dst_root = destination&nbsp; &nbsp; &nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dst_root = archive#Now its time to make the move.&nbsp; &nbsp; &nbsp; &nbsp; dst_path = os.path.join(dst_root, subdir, os.path.basename(file_path))&nbsp; &nbsp; &nbsp; &nbsp; move_file(file_path, dst_path)你可以这样实现import osimport timefrom datetime import datetimeSOURCE = r'c:\data\AS\Desktop\Source'DESTINATION = r'c:\data\AS\Desktop\Destination'ARCHIVE = r'c:\data\AS\Desktop\Archive'def get_time_difference(date, time_string):&nbsp; &nbsp; """&nbsp; &nbsp; You may want to modify this logic to change the way the time difference is calculated.&nbsp; &nbsp; """&nbsp; &nbsp; time_difference = datetime.now() - datetime.strptime(f"{date} {time_string}", "%d-%m-%Y %H:%M")&nbsp; &nbsp; hours = time_difference.total_seconds() // 3600&nbsp; &nbsp; minutes = (time_difference.total_seconds() % 3600) // 60&nbsp; &nbsp; return f"{int(hours)}:{int(minutes)}"def move_and_transform_file(file_path, dst_path, delimiter="\t"):&nbsp; &nbsp; """&nbsp; &nbsp; Reads the data from the old file, writes it into the new file and then&nbsp;&nbsp; &nbsp; deletes the old file.&nbsp; &nbsp; """&nbsp; &nbsp; with open(file_path, "r") as input_file, open(dst_path, "w") as output_file:&nbsp; &nbsp; &nbsp; &nbsp; data = {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "Date": None,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "Time": None,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "Power": None,&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; time_difference_seen = False&nbsp; &nbsp; &nbsp; &nbsp; for line in input_file:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (line_id, item, line_type, value) = line.strip().split()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if item in data:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; data[item] = value&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if not time_difference_seen and data["Date"] is not None and data["Time"] is not None:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; time_difference = get_time_difference(data["Date"], data["Time"])&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; time_difference_seen = True&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print(delimiter.join([line_id, "TimeDif", line_type, time_difference]), file=output_file)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if item == "Power":&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; value = str(int(value) * 10)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print(delimiter.join((line_id, item, line_type, value)), file=output_file)&nbsp; &nbsp; os.remove(file_path)def process_files(all_file_paths, newest_file_path, subdir):&nbsp; &nbsp; """&nbsp; &nbsp; For each file, decide where to send it, then perform the transformation.&nbsp; &nbsp; """&nbsp; &nbsp; for file_path in all_file_paths:&nbsp; &nbsp; &nbsp; &nbsp; if file_path == newest_file_path and os.path.getctime(newest_file_path) < time.time() - 120:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dst_root = DESTINATION&nbsp; &nbsp; &nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dst_root = ARCHIVE&nbsp; &nbsp; &nbsp; &nbsp; dst_path = os.path.join(dst_root, subdir, os.path.basename(file_path))&nbsp; &nbsp; &nbsp; &nbsp; move_and_transform_file(file_path, dst_path)def main():&nbsp; &nbsp; """&nbsp; &nbsp; Gather the files from the directories and then process them.&nbsp; &nbsp; """&nbsp; &nbsp; for subdir in os.listdir(SOURCE):&nbsp; &nbsp; &nbsp; &nbsp; subdir_path = os.path.join(SOURCE, subdir)&nbsp; &nbsp; &nbsp; &nbsp; if not os.path.isdir(subdir_path):&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; continue&nbsp; &nbsp; &nbsp; &nbsp; all_file_paths = [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; os.path.join(subdir_path, p)&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for p in os.listdir(subdir_path)&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if os.path.isfile(os.path.join(subdir_path, p))&nbsp; &nbsp; &nbsp; &nbsp; ]&nbsp; &nbsp; &nbsp; &nbsp; if all_file_paths:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; newest_path = max(all_file_paths, key=os.path.getctime)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; process_files(all_file_paths, newest_path, subdir)if __name__ == "__main__":&nbsp; &nbsp; main()

泛舟湖上清波郎朗

您的代码肯定只是在修改文件而不是移动它们?我想移动和修改它们。顺便说一句,如果我尝试将您的代码插入我的代码,我会得到一个无效的语法。import osimport timefrom datetime import datetimeSOURCE = r'c:\data\AS\Desktop\Source'DESTINATION = r'c:\data\AS\Desktop\Destination'ARCHIVE = r'c:\data\AS\Desktop\Archive'def get_time_difference(date, time_string):&nbsp; &nbsp; """&nbsp; &nbsp; You may want to modify this logic to change the way the time difference is calculated.&nbsp; &nbsp; """&nbsp; &nbsp; time_difference = datetime.now() - datetime.strptime(f"{date} {time_string}", "%d-%m-%Y %H:%M")&nbsp; &nbsp; hours = time_difference.total_seconds() // 3600&nbsp; &nbsp; minutes = (time_difference.total_seconds() % 3600) // 60&nbsp; &nbsp; return f"{int(hours)}:{int(minutes)}"def move_and_transform_file(file_path, dst_path, delimiter="\t"):&nbsp; &nbsp; """&nbsp; &nbsp; Reads the data from the old file, writes it into the new file and then&nbsp;&nbsp; &nbsp; deletes the old file.&nbsp; &nbsp; """&nbsp; &nbsp; with open(file_path, "r") as input_file, open(dst_path, "w") as output_file:&nbsp; &nbsp; &nbsp; &nbsp; data = {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "Date": None,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "Time": None,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "Power": None,&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; time_difference_seen = False&nbsp; &nbsp; &nbsp; &nbsp; for line in input_file:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (line_id, item, line_type, value) = line.strip().split()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if item in data:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; data[item] = value&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if not time_difference_seen and data["Date"] is not None and data["Time"] is not None:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; time_difference = get_time_difference(data["Date"], data["Time"])&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; time_difference_seen = True&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print(delimiter.join([line_id, "TimeDif", line_type, time_difference]), file=output_file)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if item == "Power":&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; value = str(int(value) * 10)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print(delimiter.join((line_id, item, line_type, value)), file=output_file)&nbsp; &nbsp; os.remove(file_path)def process_files(all_file_paths, newest_file_path, subdir):&nbsp; &nbsp; """&nbsp; &nbsp; For each file, decide where to send it, then perform the transformation.&nbsp; &nbsp; """&nbsp; &nbsp; for file_path in all_file_paths:&nbsp; &nbsp; &nbsp; &nbsp; if file_path == newest_file_path and os.path.getctime(newest_file_path) < time.time() - 120:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dst_root = DESTINATION&nbsp; &nbsp; &nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dst_root = ARCHIVE&nbsp; &nbsp; &nbsp; &nbsp; dst_path = os.path.join(dst_root, subdir, os.path.basename(file_path))&nbsp; &nbsp; &nbsp; &nbsp; move_and_transform_file(file_path, dst_path)def main():&nbsp; &nbsp; """&nbsp; &nbsp; Gather the files from the directories and then process them.&nbsp; &nbsp; """&nbsp; &nbsp; for subdir in os.listdir(SOURCE):&nbsp; &nbsp; &nbsp; &nbsp; subdir_path = os.path.join(SOURCE, subdir)&nbsp; &nbsp; &nbsp; &nbsp; if not os.path.isdir(subdir_path):&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; continue&nbsp; &nbsp; &nbsp; &nbsp; all_file_paths = [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; os.path.join(subdir_path, p)&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for p in os.listdir(subdir_path)&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if os.path.isfile(os.path.join(subdir_path, p))&nbsp; &nbsp; &nbsp; &nbsp; ]&nbsp; &nbsp; &nbsp; &nbsp; if all_file_paths:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; newest_path = max(all_file_paths, key=os.path.getctime)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; process_files(all_file_paths, newest_path, subdir)if __name__ == "__main__":&nbsp; &nbsp; main()

慕斯709654

你不能修改移动它的方式。你首先必须移动它然后你才能做你的工作。为此,您可以将文件的最终目的地(包括名称中的子目录)存储在一个数组中,稍后对其进行迭代以打开文件并完成您的工作。这是一个最小的例子def changeFile(fileName):&nbsp; &nbsp; # do your desired work here&nbsp; &nbsp; passfiles = ["dir/subdir1/file1", "dir/file"]for file in files:&nbsp; &nbsp; os.rename(file, newPath)&nbsp; &nbsp; changeFile(newPath)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python