使用codecs模块提供方法创建指定编码格式文件
open(fliename,mode,encoding.errors,buffering):使用指定编码格式打开文件
命令行参数argv
07:51
Python标准文件
Python文件属性
file.fileno() :文件描述符
file.mode() :文件打开权限
file.encoding :文件编码格式
file.closed :文件是否关闭
#默认使用只读方式打开文件
Python文件属性
file.fileno() :文件描述符
file.mode() :文件打开权限
file.encoding :文件编码格式
file.closed :文件是否关闭
#默认使用只读方式打开文件
f = open('imooc.txt') f.fileno #6 f.mode #'r' f.closed #False f.encoding #
Python 标准文件
文件标准输入 : sys.stdin; 0(fileno)
文件标准输出 : sys.stdout; 1
文件标准错误 :sys.stderr; 2
import sys
type(sys.stdin) #file
sys.stdin.mode #'r'
sys.stdin.read()
sys.stdin.fileno() #0
a= raw_input(":")
#调用sys.stdin
sys.stdout.mode #'w'
sys.stdout.write('1000')#1000
print 'hello' #调用sys.stdout
sys.stderr.mode #'w'
文件属性格式
使用普通方式打开文件(ASCII码),写入汉字会报错,使用unicode.encode(u'汉字','utf-8')转换后写入
使用codecs模块创建指定编码格式的文件
通过sys.argv可以得到命令行参数列表
raw_input(prompt)从stdin中读取字符串,sys.stdin.read()
print 调用sys.stdout.write()
打开python命令行终端时都会创建sys.stdin fileno=0,sys.stdout fileno=1,sys.stderr fileno=2三个文件
sys.argv:获取命令行参数列表
标准文件:
文件对象属性
Python文件编码格式
文件的所有属性
Python文件属性:
file.fileno():文件描述符;
file.mode:文件打开权限
file.encoding:文件编码格式
file.closed:文件是否关闭
Python标准文件
文件标准输入:sys.stdin
文件标准输出:sys.stdout
文件标准错误:sys.stderr
使用codecs模块提供方法创建指定编码格式文件
open(fliename,mode,encoding.errors,buffering):使用指定编码格式打开文件
file.fileno():文件描述符;
file.mode:文件打开权限
file.encoding:文件编码格式
file.closed:文件是否关闭
Python标准文件
文件标准输入:sys.stdin
文件标准输出:sys.stdout
文件标准错误:sys.stderr
Python文件编码格式
通过argv获取命令行参数
Python文件命令行参数
Python标准文件
Python文件属性
本节内容
1、文件对象属性
2、标准文件
3、文件命令行参数
4、文件命令行参数
5、文件编码格式
2019.6.4
f.seek 文件指针,可以跳到想要的位置,读取文件
3-1文件属性编码格式
1)sys.argv:
2)encode utf-8解决中文字符的问题。可以创建的时候先创建一个utf-8格式的文件,然后写中文就没问题了。用codecs
导入codecs模块,使用codecs.open(文件名,打开方式,文件格式(utf-8)……)即可直接写入中文字符
import codecs
f = codecs.open('test.txt','w','utf-8')
f.write(u'慕课')
f.close()
cat test.txt
python标准文件
Python文件属性
python 标准文件
File operation
file.fileno(): file description
file.mode: file open permission
file.encoding: file format
file closed : check file close or not
file.tell(): return file position
Python standard file
sys.stdin: file input
sys.stdout: file output
sys.stderr: file error
Python file command parameter
sys.argv: provide command para, which is a list
Python file format
_*_ coding:utf-8
open(fname,mode,encoding,errors,buffering)
python文件属性:
file.fileno() 文件描述符;
file.mode 文件打开权限
file.encoding 文件编码格式
file.closed 文件是否关闭