Python 2.7不会覆盖现有文件。它只会创建新的。
每个已存在的名为push.lua的文件都不会写入更改。
# Push Replacer .py
import os
file_open = open('push_new.lua', 'r')
file_contents = file_open.read()
for root, dirs, files in os.walk("."):
path = root.split(os.sep)
for file in files:
if (file == 'push.lua'):
with open(file, 'w') as f:
f.write(file_contents)
f.close()
file_open.close()
发布于 2018-08-16 01:16:42
您的代码始终在当前工作目录中打开并覆盖push.lua
,而不是在任何可能包含该名称的文件的子目录中。你需要做的不只是open(file, 'w')
,还要做open(os.path.join(root, file), 'w')
。
我怀疑您试图使用path
变量朝这个方向前进,但您从未真正使用path
变量做任何事情。
https://stackoverflow.com/questions/51867703
复制相似问题