![](/img/trans.png)
[英]How to print path of the textfile that we just write in another directory and print string in every file in Python
[英]How to write/print a string to a new textfile in python?
我在變量中有一個字符串,我想將該變量以打印形式IE寫入文本文件。我的明顯猜測是這樣做的:
'filename'.write(string_variable)
但是它給了我這個錯誤。
Traceback (most recent call last):
File "<pyshell#14>", line 1, in <module>
main('studyguide.txt','decrypted.txt')
File "C:/Users/Casey/Documents/New folder/pa11.py", line 45, in main
writefile.write(finalstr)
AttributeError: 'str' object has no attribute 'write'
有任何想法嗎?
您需要open
一個文件,並使用file.write
:
with open(writefile, 'w') as f:
f.write(finalstr)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.