Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/353.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python-用下划线替换所有空格,并将目录中的所有文件转换为小写_Python - Fatal编程技术网

Python-用下划线替换所有空格,并将目录中的所有文件转换为小写

Python-用下划线替换所有空格,并将目录中的所有文件转换为小写,python,Python,我正试图按照标题所解释的去做,但我得到了信息 WinError2:找不到指定的文件“New Text Document.txt”->“New\u Text\u Document.txt”,其代码段如下。是的,我的桌面位于驱动器号D上,这假设目标目录名为“directory”。我在目录中有一个名为“New Text Document.txt”的示例文件。我就是不知道问题出在哪里 import os path = 'D:\Desktop\directory' filenames = os.listd

我正试图按照标题所解释的去做,但我得到了信息 WinError2:找不到指定的文件“New Text Document.txt”->“New\u Text\u Document.txt”,其代码段如下。是的,我的桌面位于驱动器号D上,这假设目标目录名为“directory”。我在目录中有一个名为“New Text Document.txt”的示例文件。我就是不知道问题出在哪里

import os
path = 'D:\Desktop\directory'
filenames = os.listdir(path)
for filename in filenames:
    os.rename(filename, filename.replace(' ', '_').lower())

因为您没有为os.rename函数指定新文本Document.txt所在的目录。您可以在for循环之前添加这一行

os.chdir(path)
使用原始字符串或完整文件路径,因为定义路径的方式也会给您带来错误


path=r'D:\Desktop\directory
'或
path='D:\\Desktop\\directory'

使用完整的文件命名以实现更安全的操作系统操作:

import os
path = 'D:\\test'
for filename in os.listdir(path):
    #print(filename)
    os.rename(os.path.join(path,filename),os.path.join(path, filename.replace(' ', '_').lower())) 

使用列表理解的一行程序:

import os

directory = 'D:\Desktop\directory'

[os.rename(os.path.join(directory, f), os.path.join(directory, f).replace(' ', '_').lower()) for f in os.listdir(directory)]

如果要通过多个级别的文件夹递归执行此操作,请使用os.walk(目录)列出从answer中借用的理解:

import os

directory = r'D:\Desktop\directory'

# Use underscore? Otherwise defaults to hyphen
is_use_underscore = True
char_to_use = '_' if is_use_underscore else '-'   

print("Renaming files now!")
for root, dirs, files in os.walk(directory):
    print(f"root: {root}")
    print(f"dirs: {dirs}")
    print(f"files: {files}")

    for current_filename in files:
        new_filename = current_filename.replace(' ', char_to_use)

        print(f"current filename: {current_filename}")
        print(f"    new filename: {new_filename}")

        os.rename(
            os.path.join(root, current_filename), 
            os.path.join(root, new_filename)
        )   

print("All done!")

我不知道你的意思。。D:\Desktop\directory不是完整的文件路径吗?@Gl0rph如果使用path=''C:\Desktop\directory',则\将充当转义字符。所以它不会给你预期的结果。这就是为什么应该使用两个反斜杠“\\”来取消转义字符。或者使用任何其他方式,如前所述,检查这个哦,哇,这是一个巨大的帮助!谢谢谢里夫,这解释了为什么我一直在玩的其他一些脚本不能正常工作…@Gl0rph没问题!我很乐意帮忙。太棒了,不过我必须承认,对于像我这样的新手来说,这有点难理解。你能解释一下f在做什么吗?当然!很抱歉造成混淆,
f
只是目录中要重命名的每个文件的变量名。因此,简单地说,它适用于os.listdir(目录)(“目录中的每个文件”)。当然,可以随意使用更改/使用其他变量名,与重命名所有变量名保持一致,并使用易于理解的变量名。此代码与lsalamon答案中的代码几乎相同,他的答案可能更易于阅读(对于每个…执行此操作),在我的例子中,
f
与lsalamon的答案中的
filename
是相同的变量。啊,好的。事情开始变得有意义了。最后我用了一些非常类似于你和isalamon的答案的东西,只是为了帮助我阅读。谢谢你的解释!很好,很高兴听到你找到了对你有意义的工作,祝你好运。干杯如何使用字符串连接操作更安全?最好使用
os.path.join
或继续使用
pathlib.path