Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
129 views
in Technique[技术] by (71.8m points)

How to create file in python?

Anyone know how to use python for creation file? when i write text_file= open("productlist.txt", "w") I need create file before star use pragram, maybe python can do it independently ?

question from:https://stackoverflow.com/questions/65836116/how-to-create-file-in-python

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Your question is not really clear, try to edit it a little to make it better.

In general, you don't need to create the file before you start your program, the 'w' option will write to that file, and will create the file if it doesn't exist.

Now, if you just want to create an empty file, use the 'x' option, as follows:

f = open("yourfile.txt", "x")

You can create all the files that you want with this, even without closing the handle.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...