Python_files_operations
Files, file objects
Open (name [, mode [, BufSize]]) Eg:file = "Data.txt" f = open (file, ' r ') F = open (file, ' W ')
- ' R ': Read
- ' W ': Write
- ' A ': Append
- ' RB ': Write binary, where files in Unix can be binary, so use ' RB '
- ' U ' or ' RU ': provides universal support for line breaks and can write files executed across platforms
Line break is ' \ r \ n ' in Windows, ' \ n ' in Unix
Methods
- F.read ([n]): read at the most n bytes
- F.readline ([n]): reads up to N per line
- F.readlines ([size])
- F.write (String): Writes a string to F
- F.writelines (lines)
- F.close ()
- F.tell (): Returns the Cur pointer
- F.seek ()
Some properties of the file object:
- F.closed: Returns False if the file is turned on
- F.mode
- F.name
- F.softspace
- F.newlines
- F.encoding
How to traverse Files:
# method 1while True: Line = F.readline () If the line: break# method 2for line in F: # Process Line
Sys.stdin, Sys.stout, Sys.stderr
StdIn maps to the user's keyboard input, stdout and stderr output text to the screen eg:
Import Syssys.stdout.write ("Enter your Name:") name = Sys.stdin.readline ()
The above equivalence and Raw_input ():
Name = Raw_input ("Enter your Name:")
Raw_input does not include subsequent newline characters when read, which differs from Stdin.readline ()
The print statement can use ', ' to indicate that there is no output line break:
Print 1, 2, 3, 4# equivalent with print 1,2,pirnt 3,4
Formatted output
Print "The values are%d%7.5f%s"% (x, y, z) print "The values is {0:d} {1:7.5f} {2}". Format (z/y)
Python3 inside, print as a function of the form, as follows:
Pirnt ("The values are", X, Y, Z, end = ")
If you want to redirect the output to a file, the Python2 is as follows:
f = open ("Output.txt", "W") print >>f, "Hello World" ... f.close ()
Inside the Python3, you can do this directly via the print () function:
Print ("The values are", x,x,z, file = f)
You can also set the delimiter between elements:
Print ("The values are", X, Y, z, Sep = ', ')
The use of "'
"Can be used to do some formatted output, such as:
form = "" "Dear% (name) s,please send back my% (item) s or pay me $% (amount) 0.2f. Sincerely yours, Joe Python User "" "Print form% { ' name ': ' Mr Bush ', ' item ': ' Blender ', ' amount ': 50.00 ,}
will be output
Dear Mr bush,please send back my blender or pay me $50.00. Sincerely yours, Joe Python User
Note that the first line after the/means the first line of the newline character, or in front of the output more than a blank line. () The keywords inside will be replaced.
The same effect can be achieved with the format () function:
form = ' Dear {name}s,please send back my {item}s or pay me ${amount:0.2f}. Sincerely yours, Joe Python User "Print form.format (name =" Jack ", item =" Blender ", amount = 50)
and the template function inside the string:
Import Stringform = string. Template ("" "Dear $name, please send the back to my $item or pay me $amount. Sincerely yours, Joe Python User "" ") print Form.substitute ({' name ': ' Mr Bush ', ' Item ': ' Blender ', ' Amount ': '%0.2f '% 50.0})
Here, use $ to indicate what will be replaced.
Python file operations