使用此Python代码,我以大写形式打印文件行,但文件保持不变(小写)。
def open_f():
while True:
fname=raw_input("Enter filename:")
if fname != "done":
try:
fhand=open(fname, "r+")
break
except:
print "WRONG!!!"
continue
else: exit()
return fhand
fhand=open_f()
for line in fhand:
ss=line.upper().strip()
print ss
fhand.write(ss)
fhand.close()
您能否建议为什么文件不受影响?
Code:
def file_reader(read_from_file):
with open(read_from_file, 'r') as f:
return f.read()
def file_writer(read_from_file, write_to_file):
with open(write_to_file, 'w') as f:
f.write(file_reader(read_from_file))
Usage:
- 创建一个名为
example.txt
包含以下内容:
你好,我的名字是
德米特里
Gangan.
创建一个名为的空文件file_to_be_written_to.txt
将其添加为最后一行file_writer("example.txt", "file_to_be_written_to.txt")
你的 .py python 文件。
python <your_python_script.py>
从航站楼。
注意:它们必须位于同一文件夹中。
Result:
file_to_be_written_to.txt
:
你好,我的名字是
德米特里
Gangan.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)