gpt4 book ai didi

Python os.rename windows错误

转载 作者:太空宇宙 更新时间:2023-11-03 13:48:44 25 4
gpt4 key购买 nike

我有一些代码正在处理以将时间戳添加到 Windows 中文件的开头,但我无法使其正常工作。

for root, dirs, files in os.walk('D:\\development\\test'):
for f in files:
fullpath = os.path.join(root + os.sep, f)
print fullpath
if fullpath.endswith('txt'):
d = str(mod_date(fullpath))
dt = d.split()
newName = str(dt[1]) + '_' + f
newNameFull = os.path.join(root + os.sep, newName)
print newNameFull
os.rename(fullpath, newNameFull)

这将正确打印:

fullpath D:\development\test\New Text Document (2).txt
newNameFull D:\development\test\11:44:04.464341_New Text Document (2).txt

但是 os.rename 会给出一个 windowsError:

Traceback (most recent call last):
File "D:/Python27/Scripts/getTime.py", line 17, in <module>
os.rename(fullpath, newNameFull)
WindowsError: [Error 123] The filename, directory name, or volume label syntax is incorrect

有什么想法吗?我已经尝试了///\的多次迭代,但找不到合适的。

最佳答案

Windows 不允许文件名包含 : 字符(驱动器标识符除外)。您需要更改时间戳以使用不同的格式。

MSDN reference .

关于Python os.rename windows错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13937812/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com