繁体   English   中英

如何在python 3.5.2中使用write函数并搜索我想要准确的文件?

[英]How to use the write function in python 3.5.2 and search for the file I want to accurately?

我有一个看起来像这样的python程序:

fileName = input("What file do you want to target?(add file type i.e. 
.exe,.html,.txt)")
print ("Opening the file...")

mode = input ("w,r,r+ or a?")
if mode = "w":
writeToFile = open(fileName + w)
    fileName.write("sample text");

我想将write函数与“ w”一起使用,但是我无法将它添加到“ fileName”变量的末尾,因为它存储为字符串,因此它搜索末尾带有w的文件,即test .pyw。 顺便使用python 3.5.2。 谢谢!

"w""r"和朋友是文件打开模式

如您所指出的,您的代码会将这些字母附加到文件名之后。 这样做:

writeToFile = open(fileName,w)

模式是open()调用的第二个参数。 第一个参数没有固定值。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM