Python: 不会写 'w'
Python: Will not Write 'w'
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()
您的代码总是在当前工作目录中打开并覆盖 push.lua
,而不是在任何子目录中,因为其中可能包含具有该名称的文件。你需要做 open(os.path.join(root, file), 'w')
而不仅仅是 open(file, 'w')
.
我怀疑你正试图用你的 path
变量朝这个方向前进,但你实际上从未将 path
变量用于任何事情。
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()
您的代码总是在当前工作目录中打开并覆盖 push.lua
,而不是在任何子目录中,因为其中可能包含具有该名称的文件。你需要做 open(os.path.join(root, file), 'w')
而不仅仅是 open(file, 'w')
.
我怀疑你正试图用你的 path
变量朝这个方向前进,但你实际上从未将 path
变量用于任何事情。