3.2. File

简介:
f = open('/tmp/workfile', 'r+')
f.write('0123456789abcdef')
f.seek(5)     # Go to the 6th byte in the file
f.read(1)
f.seek(-3, 2) # Go to the 3rd byte before the end
f.read(1)
f.readline()
f.close()
		
for line in open("myfile.txt"):
    print line

with open("myfile.txt") as f:
    for line in f:
        print line
		

with open('beak', 'rb+') as f:
    content = f.read()
    f.seek(0)
    f.write(content.replace(b'\r', b''))
    f.truncate()		
		





原文出处:Netkiller 系列 手札
本文作者:陈景峯
转载请与作者联系,同时请务必标明文章原始出处和作者信息及本声明。

目录
相关文章
|
C#
C# File、FileInfo、Directory、DirectoryInfo
本文主要介绍文件类、文件信息类、目录类、目录信息类的常用属性和方法
51 0
|
6月前
|
编译器 API C语言
C/C++ 获取文件名的方法:分享一些实用的获取文件名的方法和技巧(__FILE__,__builtin_FILE(),__BASE_FILE__等)
C/C++ 获取文件名的方法:分享一些实用的获取文件名的方法和技巧(__FILE__,__builtin_FILE(),__BASE_FILE__等)
647 0
|
6月前
|
Shell
adb: error: cannot create file/directory ‘d:/1.png‘: No such file or directory
adb: error: cannot create file/directory ‘d:/1.png‘: No such file or directory
102 0
|
6月前
|
C#
C# File.Copy介绍
C# File.Copy介绍
|
6月前
|
Java
cannot open shared object file: No such file or directory
cannot open shared object file: No such file or directory
84 0
指定的 filePath 文件不存在
指定的 filePath 文件不存在
54 2
ftok info: No such file or directory
ftok info: No such file or directory
191 0
objdump: ‘1443.14.0)‘: No such file or directory
objdump: ‘1443.14.0)‘: No such file or directory
108 0
|
Android开发 C++
安卓打包失败:opening dependency file *.o.d: No such file or directory
安卓打包失败:opening dependency file *.o.d: No such file or directory
277 0
No such file or directory
No such file or directory
319 0
No such file or directory