这只有 30 行长,所以我会发布整个内容。整个脚本应该采用另一个 .py 文件并重写它,使其完全在一行上。我遇到的一个问题是,如果脚本有任何注释,它会注释掉所有其他“行”。不起作用的部分用# *这是不起作用的部分*该部分应该做的是删除该行上的 # 字符及其后面的所有内容,但它似乎没有做什么都没有。
from sys import argv
script, input_file = argv
def make_one_line(f):
# reads the file, then adds each line to a list
# then adds that line to 'final'
one_line = ''
text_body = f.read()
f.seek(0)
lines = text_body.splitlines()
lines.reverse() # this is done because pop() starts at the back
# ****THIS IS THE PART THAT DOESN'T WORK****
for line in lines:
line.split("#")
# ****THIS IS THE PART THAT DOESN'T WORK****
while lines != []:
next_one = lines.pop()
one_line += next_one
one_line += ';'
return one_line
print "This will rewrite the file, press CTRL-C to cancel."
raw_input('Press any key (but CTRL-C) to continue.')
current_file = open(input_file, 'r+')
final = make_one_line(current_file)
current_file.truncate()
current_file.seek(0) # if this isn't here, you get an error on Windows
current_file.write(final)