|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
import os
os.chdir(r'C:\Users\Administrator\Desktop')
f=open('file1.txt','r')
f2=open('file2.txt','w')
for i in f:
i=list(str(i).strip())
i.reverse()
i=''.join(i)
f2.write(i+"\n")
print(i)
f.close()
代码是没问题的。
关键不在代码,为什么用IDLE运行后,file2文件为空白,如果直接运行该文件(命名为)“file.py” 则能看到file2有符合预期的内容。
这到底是什么原因,求解。
本帖最后由 jackz007 于 2019-5-17 13:44 编辑
楼主再试试这个代码,看看是否有改观
- import sys
- f1 = open(sys . argv[0] , 'rt')
- f2 = open(r'C:\Users\Administrator\Desktop\file2.txt' , 'wt')
- for i in f1:
- i = i[::-1] . strip()
- f2 . write(i + '\n')
- print(i)
- f2 . close()
- f1 . close()
复制代码
|
|