python - 在 Python 中用汉字重命名文件
问题描述
我正在尝试重命名一些包含中文字符的文件。但是,以下内容不起作用:
import os
for filename in os.listdir(r"C:\Users\mas\Desktop\"):
if filename.startswith("你好"):
os.rename(filename, filename[7:])
它给出了错误“系统找不到指定的文件:'你好Hello.txt”
我需要在这里更改一些设置吗?
解决方案
根据错误消息,似乎找不到文件
我遇到了类似的问题,我首先通过更改当前工作目录来解决它
在你的情况下
# Change working directory first
os.chdir("C:\Users\mas\Desktop")
# then do the loop
for filename in os.listdir(r"C:\Users\mas\Desktop\"):
...
推荐阅读
- ios - 在什么情况下 App Store 收据可能会丢失或无效?
- c++ - OpenGL 视口从 -50 到 50 而不是 -1 到 1
- apache-flink - flink-sql:如何检查数组类型是否包含给定元素?
- ngx-charts - 如何在 ngx-chart 中设置主题属性?
- mobx - mobx-state-tree Map 的唯一参考
- javascript - 在 couchdb 中发出按日期排序的最新数据
- javascript - 我的标记未在 React-Leaflet 上进行地理定位
- python - 如何使用 Python 从列中拆分 2 个日期并映射到两个差异字段
- testing - 在测试时定义 grails 内存设置
- mysql - 如何获取列值并将它们粘贴到水平行中?