在Python中,文件的读写操作是实用的编程任务之一。本文将详细介绍利用python读取、写入文件的基础方法。
读取文件
在Pyhton3中,读取文件前需先打开文件。可以利用Python的open()函数打开文件。
file1=open('/User/ikun/gege.txt','r') #填入文件的路径
file2=open('gege.txt','r') #需要让txt文件与程序在同一个目录中
程序中的标识符‘r’,表示只读
如果文件在路径中不存在,则会出现IOError异常。可以用try-except语句处理报错。
用read()函数可以把文件中的内容先存储在内存,而后输出为字符串。read()函数会一次性读取整个文件,而readlines()可以逐行读取文件。
my_file=open('QRX.txt','r')
print(file.read())
print(file.readlines())
在读取文件后需要关闭文件,以减小内存占用。
my_file.close()
python中的with语句可以简化这个操作。with语句打开的文件会在程序末尾自动关闭。
Qin_ru_xin_path='/User/qin/calculus.txt'
with open(Qin_ru_xin_path,'r') as qinru:
q=qinru.read()
print(q)
#文件已被自动关闭
二进制文件
在读取图片,视频文件等二进制文件时时,需要标识符‘rb’。
pic=open('art.jpg','rb')
print(pic.read)
#返回结果将会是十六进制字节
写入文件
与读取文件相似,写入文件可以使用‘w’和‘wb’标识符。
f=open('/Users/hello/hi.txt', 'w')
f.write('Hello, world!')
f.close()
‘w’模式会让输入内容覆盖文件原有内容。如果想将内容追加在文件末尾,看使用‘a’(append)模式
Qin_ru_xin_path='/User/qin/calculus.txt'
with open(Qin_ru_xin_path,'a') as qinru:
qinru.write('微积分')
#文件已被自动关闭
ihuVkr3W4GF
This piece has opened my eyes to a whole new viewpoint; thank you.
I love how your posts always leave me motivated and inspired.
I’m startled by your capability to make even the most ordinary matters intriguing. Kudos to you!
This website is exceptional. The content are captivating and enlightening.
Your prose forms colorful pictures in my mind. I can easily visualize every detail you depict.
Your storytelling holds my attention throughout. I can’t help but read every single word you write.
Your blog posts resemble little doses of radiance that brighten up my day. Thank you for that!
This post is easy-to-read and understandable, great job.
EcQgstmyY6Q
I’m always on the lookout for top-notch blogs and this definitely one of them.
We loved reading your post from start to finish. The author have maintained me interested. Thanks for sharing your thoughts.
Love the unique angle you bring to this subject.
I have been browsing online more than 2 hours today, yet
I never found any interesting article like yours. It is pretty worth enough for me.
Personally, if all webmasters and bloggers made good content as
you did, the internet will be much more useful than ever before.
Your blog illuminates my day like a beacon. Thank you for spreading positivity with your words.
Your writing skills is incredible, I enjoyed every bit of it.
Thank you for posting this informative post. Your ideas are very thought-provoking and the writing is clearly crafted. Keep it up!
Hello! I realize this is sort of off-topic however I had to ask.
Does managing a well-established website like yours require
a massive amount work? I’m brand new to operating a blog but I do write in my diary
on a daily basis. I’d like to start a blog so I will be able
to share my own experience and views online. Please let me
know if you have any kind of suggestions or tips for new aspiring blog owners.
Appreciate it!
I truly enjoyed reading this article and learned some valuable information. Thanks for sharing your insights with your readers.
Your style captivated me from the first sentence, kudos.
I love how you break down complex ideas into simple and comprehensible parts.
Your content held my attention from beginning to end.
Great job on this insightful post. Your writing is captivating and your thoughts are clearly stated. Looking forward to more posts.
I’m captivated by your aptitude to turn mundane topics into riveting content. Great job!
Your wisdom on this subject is enlightening, kudos to your expertise.
Nice respond in return of this matter with genuine arguments and telling the whole thing on the
topic of that.
Your zeal is irresistible. It’s challenging not to be enchanted by the topics you explore.
Your writing is funny and informative, I have learnt a lot from it.