【Python】文件访问操作
本文最后更新于 1739 天前,其中的信息可能已经有所发展或是发生改变。

[successbox title=”读取文件”]
1.调用read()方法。
[sourcecode language=”python” title=”demo5.py”]
helloFile = open("H:\\demo\\demo.txt")
fileContent = helloFile.read()
helloFile.close()
print(fileContent)
[/sourcecode]
2.设置参数一次读取5个字符读取文件。
[sourcecode language=”python” title=”demo5_1.py”]
helloFile = open("H:\\demo\\demo.txt")
fileContent = ""
while True:
line = helloFile.read(5)
if line == "":
break
print(line)
fileContent += line
helloFile.close()
print(fileContent)
[/sourcecode]
3.readline()方法。
[sourcecode language=”python” title=”demo6.py”]
helloFile = open("H:\\demo\\demo.txt")
fileContent = ""
while True:
line = helloFile.readline()
if line == "":
break
print(line)
fileContent += line
helloFile.close()
print(fileContent)
[/sourcecode]
4.readlines()方法。
[sourcecode language=”python” title=”demo7.py”]
helloFile = open("H:\\demo\\demo.txt")
fileContent = helloFile.readlines()
helloFile.close()
print(fileContent)
for line in fileContent:
print(line)
[/sourcecode]
[/successbox]
[successbox title=”写入文件”]
1.写入案例。


2.write()方法。
[sourcecode language=”python” title=”demo8.py”]
helloFile = open("H:\\demo\\demo.txt","w")
helloFile.write("Hello!\nHi!\n")
helloFile.close()
helloFile = open("H:\\demo\\demo.txt","a")
helloFile.write("benzhu!")
helloFile.close()
helloFile = open("H:\\demo\\demo.txt")
fileContent = helloFile.read()
helloFile.close()
print(fileContent)
[/sourcecode]
[/successbox]
[successbox title=”复制文件”]
1.write()方法。
[sourcecode language=”python” title=”demo9_1.py”]
def copy_file(oldFile, newFile):
oldFile = open(oldFile, "r")
newFile = open(newFile, "w")
while True:
fileContent = oldFile.read(50)
if fileContent == ""
break
newFile.write(fileContent)
oldFile.close()
newFile.close()
return
copy_file("H:\\demo\\demo.txt", "H:\\demo\\demo1.txt")
[/sourcecode]
2.writelines()方法。
[sourcecode language=”python” title=”demo9_2.py”]
def copy_file(oldFile, newFile):
oldFile = open(oldFile, "r")
newFile = open(newFile, "w")
fileContent = oldFile.readlines()
print(fileContent)
newFile.writelines(fileContent)
oldFile.close()
newFile.close()
return
copy_file("H:\\demo\\demo.txt", "H:\\demo\\demo1.txt")
[/sourcecode]
[/successbox]
[successbox title=”文件内的移动”]
1.案例。

2.seek()函数设置新的文件当前位置,允许在文件中跳转,实现对文件的随机访问。
[sourcecode language=”python” title=”demo10.py”]
exampleFile = open("H:\\demo\\demo2.txt", "w")
exampleFile.write("0123456789")
exampleFile.seek(3)
exampleFile.write("benzhu")
exampleFile.close()
exampleFile = open("H:\\demo\\demo2.txt")
benzhu = exampleFile.read()
print(benzhu)
exampleFile.close()
[/sourcecode]
[/successbox]

暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇