python - 在 Python 中使用 os.rename() 时出现 FileNotFoundError
问题描述
我正在关注这篇文章来重命名文件名,但我在下面遇到错误,我尝试了很多可以在这里找到的潜在解决方案,但没有一个能解决我的问题:
import os
import datetime
current_date = datetime.datetime.today().strftime('%Y-%m-%d %H:%M:%S')
current_date
os.rename(r'C:\Users\...\xxxx.csv', r'C:\Users\...\xxxx_' + str(current_date) + '.csv'
错误是:
FileNotFoundError: [WinError 3] The system cannot find the path specified: 'C:\\Users\\...\\xxx.csv -> 'C:\\Users\\...\\xxx_2020-04-14 16:43:56.csv'
我该如何解决?
解决方案
它似乎不喜欢 current_date 的格式
import os
import datetime
current_date = datetime.datetime.today().strftime('%Y-%m-%d %H:%M:%S')
old = 'C://path//somefile.csv'
new = 'C://path//somefile'+'new.csv' #str(current_date)+'.csv'
os.rename(old,new)
这对我有用(windows),您不能在文件名中使用某些字符,在这种情况下,它是“:”导致问题,将其更改为“-”应该可以解决它。
推荐阅读
- reactjs - 社交媒体登录后如何动态重定向到私有路由?
- javascript - Javascript createelement按钮不可点击
- c# - 不要使用 c# 运行指定的 Windows 应用程序
- android - 使用 Picasso 从 Firebase 检索图像
- r - R/dplyr:如何初始化一个空的数据框列表?
- flutter - 为什么灵活的小部件不能与颤振中的列小部件一起使用
- elasticsearch - 如何更新 ElasticSearch search_analyzer 映射而不会在分析器上发生冲突?
- javascript - React.js 功能组件中如何将子组件状态传递给父组件状态?
- javascript - Vue app.js 与主页上的其他 JavaScript 文件冲突(Laravel)
- python - 从无序列表中获取路径