######################################### Python中文件的操作 #########################################################
'''
文件夹的操作:
1、创建文件夹
2、删除文件夹
'''
#在指定的目录下新建目录
import os
#os.mkdir("F:\\new") #在f盘下新建目录new
#删除指定目录下的文件夹
#os.rmdir("F:\\new") #删除new目录 由由于\n 会被系统默认换行,所以只能用\\
#创建多级目录只能重复上面的操作,一层一层的建立
#打开目录
'''
1、新建文件
2、打开文件
3、读取文件
4、在文件中写入
'''
import os
f=open("a.txt","w") #a.txt 新建的文件名 ,x新建一个文件
'''
'r' open for reading (default)
'w' open for writing, truncating the file first
'x' create a new file and open it for writing
'a' open for writing, appending to the end of the file if it exists 'b' binary mode 't' text mode (default)
'+' open a disk file for updating (reading and writing)
'U' universal newline mode (deprecated)
'''
f.write("I will run, I will climb, I will soar\n")
f.write("I'm undefeated")
f.close()
#打开文件,使用f.read()读取整个文件内容
f=open("a.txt","r")
print(f.readline().strip())
print(f.readline().strip())
f.close()
#打开文件使用f.readline()读取文件中的一行
f=open("a.txt","r")
print(f.read())
f.close()
#清除文件之前的内容,重新写入新的文件内容
f=open("a.txt","w")
f.write("我爱你中国")
f.close()
#在原文件内容的最后追加内容
f=open("a.txt","r+")
f.read()
f.write("zhujia")
f.close()
f=open("a.txt","r+")
f.readline()
f.write("zhujia")
f.close()