为了逐行而不是逐行获取输出,我想开始使用/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 不是解决方案?让我知道!
最佳答案
某些字符必须“转义”才能将它们输入到字符串中。在这种情况下,您需要一个换行符,写成 \n
在 Python 中。
无论如何,回答你的问题:
with open(completeName, "a+") as file1:
file1.write(your_pass + '\n')
这将连接 your_pass
中的字符串使用包含一个换行符的字符串,然后调用 file1.write
.
关于python - 如何在 Python3 中使用/n?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67959883/