Home > Backend Development > Python Tutorial > Python input and file operation methods

Python input and file operation methods

高洛峰
Release: 2017-03-10 18:57:30
Original
1800 people have browsed it

This article introduces python input and file operation methods

1. python input

Python3 provides an input() that allows users to input strings and store them in a variable . As shown below, the user inputs 1 from the keyboard and ends with a carriage return. The input "1" is stored in the a variable in the form of a string.

>>> a=input("Please input")
Please input1
>>> a
'1'
>>>
Copy after login

2. python file operation


1. File operation method

1.os.mknod("test.txt") 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: with Open in write mode,
a: Open in append mode (start from EOF, create new file if necessary)
r+: Open in read-write mode
w+: Open in read-write mode (see w )
a+: Open in read-write mode (see a)
rb: Open in binary read mode
wb: Open in binary write mode (see w)
ab: Open in binary append mode (see a)
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 is the length of the read, in bytes.
fp.readline([size])                                              Just a part of a line
fp.readlines([size])                   # Treat each line of the file as a member of a list and return this list. In fact, it is implemented internally by calling readline() in a loop. If the size parameter is provided, size represents the total length of the read content, which means that only a part of the file may be read.
FP.Write (STR)#Write STR into the file, WRITE () will not add a change character behind the str
FP.Writelines (SEQ)# (Multiple lines written at once). This function also just writes faithfully, without adding anything after each line.
fp.close()                           #Close the file. Python will automatically close a file after it is no longer used. However, this function is not guaranteed. It is best to develop the habit of closing it yourself. If a file is operated on after it is closed, a ValueError will occur The beginning of is the origin
fp.next()                         #Return to the next line and move the file operation mark to the next line. When a file is used in a statement such as for...in file, the next() function is called to implement traversal.
fp.seek(offset[,whence])                                                         #Move the file operation mark to the offset position. This offset is generally calculated relative to the beginning of the file, and is generally a positive number. But this is not necessarily the case if the whence parameter is provided. whence can be 0 to start the calculation from the beginning, and 1 to use the current position as the origin. 2 means the calculation is performed with the end of the file as the origin. It should be noted that if the file is opened in a or a+ mode, the file operation mark will automatically return to the end of the file every time a write operation is performed.
fp.truncate([size])                                                                     #Cut the file to the specified size. The default is to cut to the position of the current file operation mark. If size is larger than the file size, depending on the system, the file may not be changed, the file may be padded to the corresponding size with 0, or some random content may be added.
2. Directory operation method
1. Create directory
os.mkdir("file")                                                                                       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")​​​​​ #olddir and newdir can only be directories, and newdir must not exist
5. Rename files (directories)
os.rename( "oldname","newname") #Use this command for files or directories
6. Move files (directories)
shutil.move("oldpos","newpos")
7.Delete files
os.remove("file")
8. Delete directory
os.rmdir("dir")                           #Only empty directories can be deleted
shutil.rmtree("dir")                                 Directories with content can be deleted
9. Convert directories
OS.CHDIR ("PATH")#Change path

三. Read the file content according to the line

. Read the content of the file according to the line. , stored in the form of a list by row, but this has a serious flaw, that is, it may occupy too much memory and affect program performance. Therefore, you can read one row at a time. There is always only one row of data in the memory, which greatly reduces the memory overhead.

The following program is shown:


##f1 = open('haproxy.conf', 'r',encoding="utf-8")

for line in f1:

print(line)


The above is the detailed content of Python input and file operation methods. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template