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 系列 手札
本文作者:陈景峯
转载请与作者联系,同时请务必标明文章原始出处和作者信息及本声明。

目录
相关文章
|
6月前
|
Web App开发 安全 前端开发
Can‘t open this file in this folder because it contains system files
Can‘t open this file in this folder because it contains system files
|
C#
C# File、FileInfo、Directory、DirectoryInfo
本文主要介绍文件类、文件信息类、目录类、目录信息类的常用属性和方法
50 0
|
6月前
|
编译器 API C语言
C/C++ 获取文件名的方法:分享一些实用的获取文件名的方法和技巧(__FILE__,__builtin_FILE(),__BASE_FILE__等)
C/C++ 获取文件名的方法:分享一些实用的获取文件名的方法和技巧(__FILE__,__builtin_FILE(),__BASE_FILE__等)
605 0
zipfile.BadZipFile: File is not a zip file|4-21
zipfile.BadZipFile: File is not a zip file|4-21
zipfile.BadZipFile: File is not a zip file|4-21
|
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
86 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
78 0
指定的 filePath 文件不存在
指定的 filePath 文件不存在
51 2
ftok info: No such file or directory
ftok info: No such file or directory
181 0
objdump: ‘1443.14.0)‘: No such file or directory
objdump: ‘1443.14.0)‘: No such file or directory
103 0