1 file attributes
File.fileno ()--File descriptor
file.mode--file currently open permissions
file.encoding--file Encoding Format (no output indicates ASCII code)
Whether the file.closed--file is closed
>>> F.fileno ()
3
>>> F.mode
' R+ '
>>> f.encoding
>>> f.closed
False
>>> filter (lambda s:s[:2]!= ' __ ', dir (f))
[' Close ', ' Closed ', ' encoding ', ' errors ', ' Fileno ', ' flush ', ' isatty ', ' mode ', ' name ', ' newlines ', ' next ', ' read ', ' Readin To ', ' readline ', ' readlines ', ' seek ', ' softspace ', ' Tell ', ' truncate ', ' write ', ' writelines ', ' xreadlines ']
2 Standard Documents
Standard input File--sys.stdin
Standard output File--sys.stdout
Standard error File--sys.stderr
You can use these three to redirect the stdout and stderr that the Python program runs to a file
Example-redirect stdout
#stdout. py
Import Sys
print ' Dive in '
Saveout = Sys.stdout
Fsock = open (' Out.log ', ' W ')
sys.stdout = Fsock # Print will be printed to Out.log
print ' This message'll be logged instead of displayed '
Sys.stdout = Saveout
fsock.close () #还原stdout
Reference: http://blog.csdn.net/lanbing510/article/details/8487997
3 File command line arguments
Receive command-line arguments through the argv property of the Sys module. Returns a list,list[0] as the script name, list[1:] all parameters.
argv.py File Contents
# argv.py
Import Sys
print ' Test----'
Argv_list = sys.argv
Print Argv_list
Command line invocation
E:\yc_study\python\ System Learning--mu lesson net \ file processing >python argv.py 1 2 3
Test----
[' argv.py ', ' 1 ', ' 2 ', ' 3 ']
E:\yc_study\python\ System Learning--mu lesson net \ file processing >python argv.py 1 2 age=18
Test----
[' argv.py ', ' 1 ', ' 2 ', ' age=18 ']
4 file Encoding format 4.1Python character encoding detailed
Http://www.cnblogs.com/timdes1/p/7747589.html
4.2 Writing a string in utf-8 format
>>> f=open (' unicode_test.txt ', ' w+ ')
>>> F.read ()
‘‘
>>> f.write (U ' hahaha ')
Traceback (most recent):
File "<stdin>", line 1, in <module>
Unicodeencodeerror: ' ASCII ' codec can ' t encode characters in position 0-2: Ordinal not in range (128)
>>> des_str=unicode.encode (U ' hahaha ', ' utf-8 ')
>>> F.write (DES_STR)
>>> F.seek (0,os. Seek_set)
>>> F.read ()
' \xe5\x93\x88\xe5\x93\x88\xe5\x93\x88 '
4.3 Creating a document in Utf-8 format
Using the Codecs module
>>> f=codecs.open(' Create_utf_8.txt ', ' w ', ' Utf-8 ')
>>> f.encoding
' Utf-8 '
>>> F.close ()
Python file Handling--02 file attributes & Standard input/output & command line parameters & file encoding