我正在编写一个程序,它将打开一个指定的文件,然后“换行”所有比给定行长更长的行并在屏幕上打印结果。
def main():
filename = input("Please enter the name of the file to be used: ")
openFile = open(filename, 'r+')
file = openFile.read()
lLength = int(input("enter a number between 10 & 20: "))
while (lLength < 10) or (lLength > 20) :
print("Invalid input, please try again...")
lLength = int(input("enter a number between 10 & 20: "))
wr = textwrap.TextWrapper()
wr.width = lLength
wr.expand_tabs = True
wraped = wr.wrap(file)
print("Here is your output formated to a max of", lLength, "characters per line: ")
print(wraped)
main()
当我这样做而不是包装时,它将文件中的所有内容打印为带有逗号和括号的列表,而不是包装它们。