文章目录
文件操作文件的批量操作
文件操作
path
= open("student.txt", "r", encoding
="utf8")
file_data
= path
.read
(1024)
print(type(file_data
))
print(file_data
)
path
.close
()
path
= open("student.txt", "w")
path
.write
("gcl\r")
path
.close
()
path
= open("student.txt", "a")
path
.write
("gcl2")
path
.close
()
import os
os
.rename
("student.txt", "student.txt")
path
= open("./c.txt", "w")
path
.write
("5454")
path
.close
()
os
.remove
("./c.txt")
path
= os
.getcwd
()
print(path
)
os
.chdir
("../")
os
.chdir
("F:\python\STU")
path
= os
.getcwd
()
print(path
)
content
= os
.listdir
("F:\python\python学习")
print(type(content
))
print(content
)
文件的批量操作
import os
list_file
= os
.listdir
("./存储文件/")
for i
in list_file
:
index
= i
.find
(".")
left
= i
[0:index
]
right
= i
[index
:]
new_filename
= left
+ "[已被修改]" + right
os
.rename
("./存储文件/" + i
, "./存储文件/" + new_filename
)
os
.chdir
("F:/python/STU/存储文件")
list_file
= os
.listdir
()
for i
in list_file
:
name
= "尝试修改"
os
.rename
(i
, name
+ i
)
转载请注明原文地址:https://ipadbbs.8miu.com/read-29291.html