如何在Python中重命名点文件名
在 GIT 中,我们有一个名为 .gitignore 的文件,它们的技术术语是什么(这些文件可以有效地称为“文件名”),它们应该被称为什么?
我用 python 编写了一个简单的脚本来更改文件名,但是,Python 似乎无法识别以点 (.) 开头的文件夹和文件,例如 .git、.gitignore 等
import os
import glob
rootdir = 'C:\\INBOX\\POLINATE\\JUNKS\\**'
# #work on files
for filename in glob.iglob(rootdir, recursive=True):
if os.path.isfile(filename): # filter dirs
print(filename)
try:
##rename
new_filename = ''.join(random.choice(string.ascii_uppercase + string.digits) for _ in range(14)).lower()
new_extension = new_filename[0:3]
final_name = os.path.dirname(filename) + os.sep + new_filename+'.'+new_extension
os.rename(filename,final_name)
print(final_name)
except Exception as e:
#raise e
print(e)
上面的脚本适用于带有 Xn 的文件.3 格式(Xn 文件名在允许范围内,包括空格)。(三 (3) 个字母扩展名),但它留下以点 (.) 开头的文件
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
以
开头的文件
主要是隐藏的文件,在许多情况下,您不需要。在您的情况下,
.git
和.gitignore
是GIT操作的支持文件。实际上,您的程序不需要这些文件即可运行。.gitattributes
文件包含您的git信息,.gitignore
文件告诉git忽略git支持文件。您可以在同一文件夹中看到
.vs
文件。我认为您正在使用VSCODE,可能是VSCODE的信息文件。Files starting with
.
are mainly hidden files that you don't need in many cases.In your case
.git
and.gitignore
are the supporting files for git operations. Actually your program dont need these files to run..gitattributes
files contains your git informations and.gitignore
file tells git to ignore the git supporting files.You can see a
.vs
file in the same folder. I assume you are using vscode and it might be the information files of vscode.