我想向文件添加特定的扩展名(可能有也可能没有扩展名),我尝试了以下代码:
import os
thisFile = "/home/username/myfile.txt"
base = os.path.splitext(thisFile)[0]
os.rename(thisFile, base + ".doc")
还有其他办法吗?
最佳答案
好吧,没有理由使用 base
变量...
os.rename(thisFile, os.path.splitext(thisFile)[0] + ".doc")
但除此之外,我觉得还不错。
关于python - 如何在 python 中添加/替换文件扩展名?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21737021/