This article introduces python input and file operations. This article introduces python input and file operations.
1. python input
Python3 provides an input () that allows you to input strings and store them in a variable. As shown in the following figure, the user enters 1 on the keyboard and ends with a carriage return. the input "1" is stored in variable a as a string.
>>> a=input("Please input")Please input1>>> a'1'>>>
2. python file operations
I. file operations
1. OS. mknod ("test.txt") to create an empty file
2. fp = open ("test.txt", w) open a file directly. if the file does not exist, create the file.
3. about open mode:
W: open in write mode,
A: open in append mode (create a file when necessary, starting from EOF)
R +: open in read/write mode
W +: open in read/write mode (see w)
A +: open in read/write mode (see)
Rb: enabled in binary read mode
Wb: open in binary write mode (see w)
AB: open in binary append mode (see)
Rb +: open in binary read/write mode (see r +)
Wb +: open in binary read/write mode (see w +)
AB +: open in binary read/write mode (see a +)
Fp. read ([size]) # size indicates the read length, in bytes.
Fp. readline ([size]) # read a row. if the size is defined, only one part of the row may be returned.
Fp. readlines ([size]) # use each row of the file as a member of a list and return this list. In fact, it is implemented by calling readline () cyclically. If the size parameter is provided, size indicates the total length of the read content, that is, it may be read only to a part of the file.
Fp. write (str) # write str to the file. write () does not add a linefeed after str.
Fp. writelines (seq) # write all seq content to a file (multiple rows are written at one time ). This function is only faithfully written without adding anything to the end of each line.
Fp. close () # close the file. Python will automatically close the file after a file is not used, but this function is not guaranteed, it is best to develop your own habit of closing. If a file is closed and operated on it, ValueError is generated.
Fp. flush () # write the buffer content to the hard disk
Fp. tell () # return the current position of the file operation mark, starting with the file as the origin
Fp. next () # return the next line and move the operation mark of the file to the next line. Use a file... When a statement such as in file is called, the next () function is called to implement traversal.
Fp. seek (offset [, whence]) # Mark the file operation and move it to the offset position. This offset is generally calculated relative to the beginning of the file, and is generally a positive number. However, if the whence parameter is provided, it is not necessary. if The whence parameter is 0, it indicates that the calculation starts from the beginning, and 1 indicates that the calculation is based on the current position. 2 indicates that the origin is the end of the file. Note that if the file is opened in a or a + mode, the operation mark of the file is automatically returned to the end of the file each time the file is written.
Fp. truncate ([size]) # crop the file to the specified size. the default value is the location marked by the current file operation. If the size is larger than the file size, the file may not be changed depending on the system, or 0 may be used to fill the file with the corresponding size, it may also be added with random content.
II. Directory operations
1. create a directory
OS. mkdir ("file ")
2. copy the file:
Shutil. copyfile ("oldfile", "newfile") # both oldfile and newfile can only be files.
Shutil. copy ("oldfile", "newfile") # oldfile can only be a folder. newfile can be a file or a target directory.
3. copy the folder:
4. shutil. copytree ("olddir", "newdir") # both olddir and newdir can only be directories, and newdir must not exist
5. rename the file (directory)
OS. rename ("oldname", "newname") # This command is used for files or directories.
6. move a file (directory)
Shutil. move ("oldpos", "newpos ")
7. delete an object
OS. remove ("file ")
8. delete a directory
OS. rmdir ("dir") # only empty directories can be deleted.
Shutil. rmtree ("dir") # you can delete empty directories and directories with content.
9. conversion Directory
OS. chdir ("path") # Change path
3. reading file content by row
You can call the readlines () function to read all the file content by row and save it as a list by row. However, a serious defect is that the memory usage may be too large, affects program performance. Therefore, one row can be read at a time. There is always only one row of data in the memory, which greatly reduces the memory overhead.
The procedure is as follows:
F1 = open ('haproxy. Conf', 'R', encoding = "UTF-8 ")
For line in f1:
Print (line)
The above is the details of the python input and file operation methods. For more information, see other related articles in the first PHP community!