文件读写

2017129

1:38

python简单文件操作

 

python操作文件常用指令

f=open("F:\\python36\\1.txt","r")

result=f.read()

print(result)

#实现读取和打印文件内容

文件的复制

f=open("F:\\python36\\1.txt","r")

result=f.read()

print(result)

s=open("F:\\python36\\2.txt","r+")

s.write(result)

b=s.read()

print(b)

#

#

old_file_name=input("请输入要复制的文件名:")

old_file=open(old_file_name,"r")

position=old_file_name.rfind(".")

new_file_name=old_file_name[:position] +"[复件]"+old_file_name[position:]

new_file=open(new_file_name,"w")

while True:

        content=old_file.read(1024)#文件处理时进行对字节的限制

        if len(content)==0:

               break

        new_file.write(content)

old_file.close()

new_file.close()

#(采用循环确保文件内内容被全部读取,如果读取长度为字节为0,跳出)

文件定位

f=open("1.txt")

f.seek(2,0)

content=f.readline()

print(content)

#(后面可以用0,1,2分别代表文件开头,当前,末尾,前面的数字表示跳过的字节)

相关文章: