文件名、目录名或卷标语法不正确:':' Python

The filename, directory name, or volume label syntax is incorrect: ':' Python

我正在尝试向我的文件管理系统添加一些新功能。但我对以下内容感到震惊。我有 3 个文件夹(源、目标和存档)。它们每个都有 3 个子文件夹(A、B 和 C)。只有 Source 中的子文件夹包含 1 个或多个文件。这些将在目标或存档中重写(移动)(取决于某些要求)。

如果满足以下条件,Source 中的子文件夹(A、B 和 C)中的文件只会被重写到 Destination 中的子文件夹(A、B 和 C)中:

  1. 它们最后创建的文件并且如果它们至少存在 120 秒。

如果它们不是最后创建的文件但它们至少 120 秒,它们将被移动到子存档中的文件夹(A、B 和 C)。

如果它们不是最后创建的文件并且它们不是至少 120 秒,它们将留在当前子-Source

中的文件夹(A、B 和 C)

重写过程中,文件中Power的内容会乘以10。 [![看起来像这样:https://i.stack.imgur.com/ee0r7.png]

这是我的代码,我收到以下错误:The filename, directory name, or volume label syntax is incorrect: ':'

谁能帮忙告诉我哪里做错了,为什么什么都没有 re-written/moved 或删除?非常感谢!

import os, os.path
import time

#Make source, destination and archive paths.
src = r'c:\data\AM\Desktop\Source'
dst = r'c:\data\AM\Desktop\Destination'
arc = r'c:\data\AM\Desktop\Archive'

os.chdir(src)

#Now we want to get the absolute paths of the files which will be used to re-write the files.
for root, subdirs, files in os.walk('.'):
    src_path = os.path.join(src, root)
    dst_path = os.path.join(dst, root)
    arc_path = os.path.join(arc, root)


    for f in files:
        src_fpath = os.path.join(src_path, f)
        dst_fpath = os.path.join(dst_path, f)
        arc_fpath = os.path.join(arc_path, f)

#Get only the newest files inside the src_fpath and store it in newest_file_paths.
        newest_file_paths = max(src_fpath, key=os.path.getctime)


#Now we start reading data from the old files, write it into the new files and delete the old files.        
        with open(src_fpath, 'r') as read1, open(dst_fpath, 'w') as write1, open(arc_fpath, 'w') as write2:
            data = {
                    'Power': None,
                    }

            for line in read1:
                splitter = (ID, Item, Content, Status) = line.strip().split()
#If the file(s) ARE the last created file(s) AND if they are at least 120 seconds old. Rewrite in Destination and remove in Source.
                if read1 == newest_file_paths and os.path.getctime(newest_file_paths) < time.time() - 120 and Item in data: 
                    Content = str(int(Content) * 10)
                    write1.write(ID+'\t'+Item+'\t'+Content+'\t'+Status+'\n') 
                    write1.write(line)
                    os.remove(src_fpath)
#If the file(s)  ARE NOT the last created file(s) but they ARE at least 120 seconds old. Rewrite in Archive and remove in Source                   
                elif read1 == newest_file_paths and os.path.getctime(newest_file_paths) > time.time() - 120 and Item in data:
                    write2.write(line)
                    os.remove(src_fpath)
#If they ARE NOT the last created file and they ARE NOT at least 120 seconds old. Stay in Source.                   
                else:
                    continue

这样的事情可能会让你开始理解我在评论中的意思:

import os, os.path
import time

def rewrite(src_fpath, dst_fpath):
    # Ensure the directory exists:
    os.makedirs(os.path.dirname(dst_fpath), exist_ok=True)

    with open(src_fpath, "r") as read1, open(dst_fpath, "w") as write1:
        for line in read1:
            (ID, Item, Content, Status) = line.strip().split()
            Content = str(int(Content) * 10)
            write1.write(ID + "\t" + Item + "\t" + Content + "\t" + Status + "\n")
            write1.write(line)
    print("Wrote", dst_fpath, "based on", src_fpath)
    # Uncomment this to actually remove the originals:
    # os.remove(src_fpath)



def process(src_dir, dst_dir, arc_dir, max_age=120):
    operations = []
    for dirpath, dirnames, filenames in os.walk(src_dir):
        if not filenames:
            # Empty directory, skip
            continue
        rel_dir = os.path.relpath(dirpath, src_dir)
        filename_to_ctime = {
            filename: os.path.getctime(os.path.join(dirpath, filename))
            for filename in filenames
        }
        newest_ctime = max(filename_to_ctime.values())
        for filename, ctime in filename_to_ctime.items():
            abspath = os.path.join(dirpath, filename)
            age = time.time() - ctime
            # TODO: is this logic correct?
            if age >= max_age:
                # Move file(s) with newest ctime to dst_dir, everything else to arc_dir
                new_root = dst_dir if ctime == newest_ctime else arc_dir
                new_path = os.path.join(new_root, rel_dir, filename)
                operations.append(("rewrite", abspath, new_path))
            else:
                operations.append(("noop", abspath))
    for operation, *args in operations:
        print(operation, args)
        if operation == "rewrite":
            rewrite(args[0], args[1])


if __name__ == "__main__":
    process(src_dir="./src", dst_dir="./dst", arc_dir="./arc")

例如,如果我有这样的结构

|____src
| |____a
| | |____2.txt
| | |____1.txt
| |____b
| | |____5-old.txt
| | |____4.txt
| | |____3.txt

其中5-old.txt是人为变老的,

输出是

('rewrite', './src/a/1.txt', './arc/a/1.txt')
('rewrite', './src/a/2.txt', './dst/a/2.txt')
('rewrite', './src/b/3.txt', './arc/b/3.txt')
('rewrite', './src/b/4.txt', './dst/b/4.txt')
('noop', './src/b/5-old.txt')