Also use open function .
open(r".\newfile.txt","x")
Files will be created in the same directory newfile.txt.
however , If newfile.txt Already exist , May be an error :
Traceback (most recent call last):
File "C:\Users\***\Desktop\MEME\1.py", line 1, in <module>
open(r".\newfile.txt","x")
FileExistsError: [Errno 17] File exists: '.\\newfile.txt'
therefore , A good solution :
import os
if not os.path.exists(r".\newfile.txt"):
open(r".\newfile.txt","x")
Use os The module determines whether there is , Create if it does not exist .
w(write) Pattern , In theory, it is a write mode , But you can also create files .
Its greatest advantage is : You can create an existing file without reporting an error .
however : Be careful !!!! The data of the existing file will be cleared
-_-||
So please be careful with .
Write method :
a=open(r".\ty.txt","w")
a.write("texture")
That's all .
however : It will cover the original content .( There are some shortcomings in the beauty )
a(append) Mode will not be overwritten , Instead, add... At the end . however , If a The schema encountered a situation where the file does not exist , Will report a mistake ( No files found ).
a=open(r".\ty.txt","a")
a.write("texture")
This line of code will add... At the end of the file 【texture】 Text .
---------------------------------------------- End -----------------------------------------------------------------------------
author: Unconquerable&Llxy
Unconquerable&Llxy The blog of _CSDN Blog -Python From negative infinity to ~,Vpython-3D,our project1 Domain Blogger Unconquerable&Llxy Good at Python From negative infinity to ~,Vpython-3D,our project1, And so on ,Unconquerable&Llxy Focus on python field .https://blog.csdn.net/html_finder?type=blog Welcome to visit ^_^