此脚本应该删除文件,而是写入3行:
from sys import argv
script, filename = argv
filesource = "C:\Users\Miguel\Downloads\Python\%s" % filename
txt = open(filesource)
print filesource
print txt.read()
print "Let's delete the file"
raw_input("Delete? Use Ctrl+C to go back")
target = open(filesource, 'r+')
target.truncate(1)
print "Provide 3 lines for the file"
line1 = "aaaaaaa"
line2 = "bbbbbbb"
line3 = "ccccccc"
target.write (("%s\n%s\n%s\n") % (line1, line2, line3))
print target.read()
target.close()
然而它给了我三条线和很多胡言乱语。
请帮忙吗?
答案 0 :(得分:1)
使用$("#saved-education").click(function (e) {
e.preventDefault();
....
....
})
模式而不是w+
,因此文件在打开时会自动截断。
然后你需要在写完后回放,这样你才能阅读刚才写的内容。
r+
答案 1 :(得分:0)
删除filesource使用:
import os
os.remove(filesource)
你还打开文件源twise witiout,将其关闭为txt和目标
编辑(只删除文件内容而不删除它):
target = open(filesource, 'w+')
print "Provide 3 lines for the file"
line1 = "aaaaaaa"
line2 = "bbbbbbb"
line3 = "ccccccc"
target.write (("%s\n%s\n%s\n") % (line1, line2, line3))
target.seek(0)
print target.read()
target.close()