Windows10下使用shutil.copy生成文件名前缀含点的问题求助
解决Windows10中Python脚本生成带前导点文件名的问题
我之前在Windows环境下处理文件操作时也遇到过类似的奇怪命名问题,咱们一步步来排查和解决你的问题:
可能的原因分析
你遇到的.followpoint-1.png而非预期followpoint-1.png的问题,大概率和以下两个因素有关:
- 当前工作目录不匹配:脚本运行时的当前路径不是你存放源文件(
followpoint.png等)的皮肤文件夹,导致文件操作的路径解析出现异常 - 源文件实际名称异常:Windows默认隐藏文件扩展名和隐藏项目,可能你看到的
followpoint.png实际是带前导点的.followpoint.png(比如命名时误加了点,或者系统显示问题)
具体解决方案
1. 先确认当前工作目录和源文件状态
在代码开头添加一行打印当前路径的代码,同时检查源文件的真实名称:
from shutil import copy import os # 打印当前工作目录,确认是否是皮肤文件夹 print("Current working directory:", os.getcwd()) # 检查源文件是否存在 for fname in ["followpoint.png", "followpoint@2x.png", "followpointblank.png"]: print(f"File {fname} exists:", os.path.exists(fname)) # 后续原代码...
运行后查看输出:
- 如果当前路径不是皮肤文件夹,需要切换到对应目录再运行脚本
- 如果源文件不存在,说明你放错了文件位置,或者文件名称有误
2. 修改代码使用绝对路径(推荐)
为了彻底避免路径问题,修改代码使用脚本所在目录的绝对路径来定位源文件,这样不管在哪里运行脚本都能正确找到文件:
from shutil import copy import os # 获取脚本所在的绝对目录 script_dir = os.path.dirname(os.path.abspath(__file__)) print("Preparation: put 3 followpoint files in your skin's folder; followpoint.png, followpoint@2x.png, and followpointblank.png. I think these names are self explanatory enough.") thingything = input("Enter your animation pattern (. = blank, - = line) (example: ...------..): ") inputs = list(thingything) for i in range(len(inputs)): target_base = f"followpoint-{i+1}" target_png = f"{target_base}.png" target_2x_png = f"{target_base}@2x.png" if inputs[i] == ".": # 拼接源文件的绝对路径 source_blank = os.path.join(script_dir, "followpointblank.png") copy(source_blank, target_png) elif inputs[i] == "-": source_normal = os.path.join(script_dir, "followpoint.png") copy(source_normal, target_png) source_normal_2x = os.path.join(script_dir, "followpoint@2x.png") copy(source_normal_2x, target_2x_png) # 删除源文件也使用绝对路径 for fname in ["followpoint.png", "followpoint@2x.png", "followpointblank.png"]: os.remove(os.path.join(script_dir, fname))
3. 检查Windows文件显示设置
打开文件资源管理器,点击顶部的「查看」选项卡,勾选:
- 文件扩展名:这样能看到文件的完整名称(比如不会把
followpoint.png.png显示成followpoint.png) - 隐藏项目:这样能看到带前导点的隐藏文件,确认你的源文件没有被误命名为
.followpoint.png
内容的提问来源于stack exchange,提问作者Henry Segal
相关产品推荐
相关产品推荐

