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

Python 在从一个文件夹传输到另一个文件夹的过程中修改多个文件,python,file,file-management,Python,File,File Management,现在我有一个程序,可以将文件从源文件夹的子目录移动到目标文件夹的子目录。这些文件包含如下信息: 现在,在从源移动到目标的过程中,我想修改两个位置上的移动文件 我想复制时间并将其粘贴为Time下的TimeDif。类型保持为Y,值必须为当前时间-时间值 我想修改幂*10的值。如果幂的值=

现在我有一个程序,可以将文件从源文件夹的子目录移动到目标文件夹的子目录。这些文件包含如下信息:

现在,在从源移动到目标的过程中,我想修改两个位置上的移动文件

  • 我想复制时间并将其粘贴为Time下的TimeDif。类型保持为Y,值必须为当前时间-时间值
  • 我想修改幂*10的值。如果幂的值=<1000,则type保持为N,否则幂的类型保持为Y
因此,将文件从源移动到目标后,它必须如下所示:

这是我现在用来移动文件的代码,所有的移动都很顺利。当我想修改文件内容时,我不知道从哪里开始:

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)
导入操作系统,操作系统路径
导入时间
#创建源、目标和归档路径。
source=r'c:\data\AS\Desktop\source'
destination=r'c:\data\AS\destination\destination'
archive=r'c:\data\AS\Desktop\archive'
创建目录路径并确保只考虑源目录下的目录。
对于os.listdir(源)中的子目录:
subdir_path=os.path.join(源,subdir)
如果不是os.path.isdir(子目录路径):
持续
#现在我们想要得到这些目录中文件的绝对路径
#并将它们存储在列表中。
all_file_path=[os.path.join(subdir_path,file)用于os.listdir(subdir_path)中的文件]
all_file_path=[p表示所有_file_路径中的p,如果os.path.isfile(p)]
#排除空子目录
如果len(所有文件路径)==0:
持续
#仅获取这些目录的最新文件。
最新的\u文件\u路径=max(所有\u文件\u路径,key=os.path.getctime)
#现在我们正在选择要移动的文件
#并为它们创建一个目标路径。
对于所有文件路径中的文件路径:
如果文件路径==最新的文件路径和os.path.getctime(最新的文件路径)
您不能在移动它的过程中进行修改。你必须先移动它,然后才能完成你的工作。为此,您可以将文件的最终目的地(包括名称中的子目录)存储在一个数组中&稍后迭代以打开文件并执行您的工作

这里有一个最小的例子

def changeFile(文件名):
#在这里做你想做的工作
通过
files=[“dir/subdir1/file1”,“dir/file”]
对于文件中的文件:
重命名(文件,newPath)
更改文件(新路径)

如果文件很小,则不移动文件,只需执行以下操作:

  • 从所有文件中读取信息
  • 查找要替换的数据
  • 在源目录中写入包含新数据的文件
  • 删除旧文件
  • 差不多

    def move_file(file_path, dst_path):
      with open(file_path, "r") as input_file, open(dst_path, "w") as output_file:
          for line in input_file:
             if <line meets criteria to modify>:
                 <modify_line>
             print(line, file=output_file)
          for <data> in <additional_data>:
             print(<data>, file=output_file)
    
      # remove the old file
      os.remove(file_path)
    
    def move_文件(文件路径、dst路径):
    将open(file_path,“r”)作为输入_文件,open(dst_path,“w”)作为输出_文件:
    对于输入_文件中的行:
    如果:
    打印(行,文件=输出文件)
    例如:
    打印(,文件=输出文件)
    #删除旧文件
    删除(文件路径)
    
    然后调用move_file函数,而不是原始代码中的os.rename

    #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))
            move_file(file_path, dst_path)
    
    
    #现在我们正在选择要移动的文件
    #并为它们创建一个目标路径。
    对于所有文件路径中的文件路径:
    如果文件路径==最新的文件路径和os.path.getctime(最新的文件路径)
    您可以像这样实现它

    import os
    import time
    from datetime import datetime
    
    SOURCE = 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):
        """
        You may want to modify this logic to change the way the time difference is calculated.
        """
        time_difference = datetime.now() - datetime.strptime(f"{date} {time_string}", "%d-%m-%Y %H:%M")
        hours = time_difference.total_seconds() // 3600
        minutes = (time_difference.total_seconds() % 3600) // 60
        return f"{int(hours)}:{int(minutes)}"
    
    def move_and_transform_file(file_path, dst_path, delimiter="\t"):
        """
        Reads the data from the old file, writes it into the new file and then 
        deletes the old file.
        """
        with open(file_path, "r") as input_file, open(dst_path, "w") as output_file:
            data = {
                "Date": None,
                "Time": None,
                "Power": None,
            }
            time_difference_seen = False
            for line in input_file:
                (line_id, item, line_type, value) = line.strip().split()
                if item in data:
                    data[item] = value
                    if not time_difference_seen and data["Date"] is not None and data["Time"] is not None:
                        time_difference = get_time_difference(data["Date"], data["Time"])
                        time_difference_seen = True
                        print(delimiter.join([line_id, "TimeDif", line_type, time_difference]), file=output_file)
                    if item == "Power":
                        value = str(int(value) * 10)
                print(delimiter.join((line_id, item, line_type, value)), file=output_file)
    
        os.remove(file_path)
    
    def process_files(all_file_paths, newest_file_path, subdir):
        """
        For each file, decide where to send it, then perform the transformation.
        """
        for file_path in all_file_paths:
            if file_path == newest_file_path and os.path.getctime(newest_file_path) < time.time() - 120:
                dst_root = DESTINATION
            else:
                dst_root = ARCHIVE
    
            dst_path = os.path.join(dst_root, subdir, os.path.basename(file_path))
            move_and_transform_file(file_path, dst_path)
    
    def main():
        """
        Gather the files from the directories and then process them.
        """
        for subdir in os.listdir(SOURCE):
            subdir_path = os.path.join(SOURCE, subdir)
            if not os.path.isdir(subdir_path):
                continue
    
            all_file_paths = [
                os.path.join(subdir_path, p) 
                for p in os.listdir(subdir_path) 
                if os.path.isfile(os.path.join(subdir_path, p))
            ]
    
            if all_file_paths:
                newest_path = max(all_file_paths, key=os.path.getctime)
                process_files(all_file_paths, newest_path, subdir)
    
    if __name__ == "__main__":
        main()
    
    导入操作系统
    导入时间
    从日期时间导入日期时间
    SOURCE=r'c:\data\AS\Desktop\SOURCE'
    DESTINATION=r'c:\data\AS\DESTINATION\DESTINATION'
    ARCHIVE=r'c:\data\AS\Desktop\ARCHIVE'
    def获取时间差(日期、时间字符串):
    """
    您可能需要修改此逻辑以更改计算时差的方式。
    """
    time_difference=datetime.now()-datetime.strtime(f“{date}{time_string},%d-%m-%Y%H:%m”)
    小时=时差。总秒数()//3600
    分钟=(时差。总秒数()%3600)//60
    返回f“{int(小时)}:{int(分钟)}”
    定义移动和转换文件(文件路径、dst路径、分隔符=“\t”):
    """
    从旧文件读取数据,将其写入新文件,然后
    删除旧文件。
    """
    将open(file_path,“r”)作为输入_文件,open(dst_path,“w”)作为输出_文件:
    数据={
    “日期”:无,
    “时间”:无,
    “权力”:无,
    }
    所见时差=假
    对于输入_文件中的行:
    (行id、项目、行类型、值)=line.strip().split()
    如果数据中有项目:
    数据[项目]=值
    如果未看到时间差且数据[“日期”]不为无且数据[“时间”]不为无:
    时差=获取时差(数据[“日期”],数据[“时间”])
    所见时差=真
    打印(delimiter.join([line\u id,“TimeDif”,line\u type,time\u difference]),file=output\u file)
    如果项目==“电源”:
    数值=str(整数(数值)*10)
    打印(delimiter.join((行id、项、行类型、值)),文件=输出文件)
    删除(文件路径)
    def进程文件(所有文件路径、最新文件路径、子目录):
    """
    对于每个文件,决定将其发送到哪里,然后执行转换。
    """
    对于所有文件路径中的文件路径:
    如果文件路径==最新文件路径和os.path.getctime(最新文件路径)import os
    import time
    from datetime import datetime
    
    SOURCE = 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):
        """
        You may want to modify this logic to change the way the time difference is calculated.
        """
        time_difference = datetime.now() - datetime.strptime(f"{date} {time_string}", "%d-%m-%Y %H:%M")
        hours = time_difference.total_seconds() // 3600
        minutes = (time_difference.total_seconds() % 3600) // 60
        return f"{int(hours)}:{int(minutes)}"
    
    def move_and_transform_file(file_path, dst_path, delimiter="\t"):
        """
        Reads the data from the old file, writes it into the new file and then 
        deletes the old file.
        """
        with open(file_path, "r") as input_file, open(dst_path, "w") as output_file:
            data = {
                "Date": None,
                "Time": None,
                "Power": None,
            }
            time_difference_seen = False
            for line in input_file:
                (line_id, item, line_type, value) = line.strip().split()
                if item in data:
                    data[item] = value
                    if not time_difference_seen and data["Date"] is not None and data["Time"] is not None:
                        time_difference = get_time_difference(data["Date"], data["Time"])
                        time_difference_seen = True
                        print(delimiter.join([line_id, "TimeDif", line_type, time_difference]), file=output_file)
                    if item == "Power":
                        value = str(int(value) * 10)
                print(delimiter.join((line_id, item, line_type, value)), file=output_file)
    
        os.remove(file_path)
    
    def process_files(all_file_paths, newest_file_path, subdir):
        """
        For each file, decide where to send it, then perform the transformation.
        """
        for file_path in all_file_paths:
            if file_path == newest_file_path and os.path.getctime(newest_file_path) < time.time() - 120:
                dst_root = DESTINATION
            else:
                dst_root = ARCHIVE
    
            dst_path = os.path.join(dst_root, subdir, os.path.basename(file_path))
            move_and_transform_file(file_path, dst_path)
    
    def main():
        """
        Gather the files from the directories and then process them.
        """
        for subdir in os.listdir(SOURCE):
            subdir_path = os.path.join(SOURCE, subdir)
            if not os.path.isdir(subdir_path):
                continue
    
            all_file_paths = [
                os.path.join(subdir_path, p) 
                for p in os.listdir(subdir_path) 
                if os.path.isfile(os.path.join(subdir_path, p))
            ]
    
            if all_file_paths:
                newest_path = max(all_file_paths, key=os.path.getctime)
                process_files(all_file_paths, newest_path, subdir)
    
    if __name__ == "__main__":
        main()