为了逐行而不是一行接一行地获得输出,我想开始使用/n命令。
以下是我认为应该放置的代码:
password_for = input('This password is for: ')
your_pass = 'Your password for {} is: {}'.format(password_for, password)
save_path = '/Users/"MyUsername"/Desktop'
name_of_file = input("What is the name of the file: ")
completeName = os.path.join(save_path, name_of_file+".txt")
with open(completeName, "a+") as file1:
file1.write(your_pass)
/n命令应该用于获取应该写入(输出(的文本,逐行如下:
Input 1
input 2
但现在输出是这样的:
Input1Input2
也许/N不是解决方案?让我知道!
某些字符必须是"逃脱";以便将它们输入到字符串中。在这种情况下,您需要一个换行符,它是用Python编写的n
。
无论如何,为了回答您的问题:
with open(completeName, "a+") as file1:
file1.write(your_pass + 'n')
这将把your_pass
中的字符串与包含一个换行符的字符串连接起来,然后调用file1.write
。
password_for = input('This password is for: ')
your_pass = 'Your password for {} is: {}'.format(password_for, password)
save_path = '/Users/"MyUsername"/Desktop'
name_of_file = input("What is the name of the file: ")
completeName = os.path.join(save_path, name_of_file+".txt")
with open(completeName, "a+") as file1:
file1.write(your_pass + 'n') # You need to place 'n' here.
您只需要将其添加到代码中:-
+ 'n'
在最后一行:-
file1.write(your_pass + 'n')
您可以将它与字符串串联运算符一起使用,即">+运算符";如下所示:
file1.write(your_pass + 'n')