python中的文件操作

#########################################  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()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值