我有一些代码,我正在工作,以添加时间戳到一个文件的开始在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有什么想法吗?我已经尝试了/\的多次迭代,但是没有找到正确的。
发布于 2012-12-19 00:59:57
Windows不允许文件名包含:字符(驱动器标识符中除外)。您需要更改时间戳以使用不同的格式。
MSDN reference。
https://stackoverflow.com/questions/13937812
复制相似问题