python-create new text, copy text to specified path

Directly upload the code, the explanation is in the comments, if you don’t understand the comments, you must reply

#创建一个text.txt 文件

#with open('路径',"w/a/r等等")  as f
# f 相当于文件别名,可自定义命名。用于f.close()  f.操作()等等

#打开一个文件,常用的操作就是w a r

#w打开一个文件只用于写入,如果改文件已存在则覆盖,不存在,则创建

#a打开一个文件用于文本追加,如果文件已存在,新内容写在文件内容之后,
#没有文件,则创建新文件

#r以只读方式打开文件

with open('C:/Users/Lenovo/Desktop/text.txt',"w") as f :
    f.write("hello world")
    
#打开两个文件:地址可以改变
f  =  open('C:/Users/Lenovo/Desktop/text.txt','r')
f1 =  open('C:/Users/Lenovo/Desktop/text1.txt','w') 

#循环复制拷贝内容
for i in f:
    f1.write(i)

#关闭文件    
f.close()
f1.close()

Guess you like

Origin blog.csdn.net/weixin_43402353/article/details/109138928