使用Python更改NAS上的文件名

2024-04-20 07:01:47 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个python脚本,当要扫描的文件夹位于本地笔记本电脑上时,它可以很好地查找指定的字符或短语,并用其他信息替换这些字符或短语(在大多数情况下,我只是删除字符)

当尝试更改其指向my NAS上文件夹的路径时,文件名不会更新,而pycharm会返回成功的代码

如果可能的话,我希望避免将GBs大小的数据复制到我的笔记本电脑上进行修改,然后将其推回到NAS

以下是脚本内容:

import os

paths = (os.path.join(root, filename)
        for root, _, filenames in os.walk('afp://NAS-SYN._afpovertcp._tcp.local/Home/Documents/Finances')
        for filename in filenames)

# The keys of the dictionary are the values to replace, each corresponding
# item is the string to replace it with
replacements = {'&': '',
                'Hello.Everyone.':'Hello Everyone - '}


for path in paths:
    # Copy the path name to apply changes (if any) to
    newname = path
    # Loop over the dictionary elements, applying the replacements
    for k, v in replacements.items():
        newname = newname.replace(k, v)
    if newname != path:
        os.rename(path, newname)

Tags: thetopathin脚本文件夹foros