Home Backend Development Python Tutorial How to read, write and create files in Python

How to read, write and create files in Python

Feb 23, 2017 am 11:09 AM
python Create method

The operation of files and folders (file operation functions) in python requires the os module and shutil module.

Get the current working directory, that is, the directory path where the current Python script works: os.getcwd()

Return all files and directory names in the specified directory: os.listdir()

The function is used to delete a file: os.remove()

Delete multiple directories: os.removedirs(r "c:\python")

The test is given Whether the path is a file: os.path.isfile()

Check whether the given path is a directory: os.path.isdir()

Judge whether it is an absolute path: os .path.isabs()

Check whether the given path actually exists: os.path.exists()

Return the directory name and file name of a path: os.path.split( ) eg os.path.split('/home/swaroop/byte/code/poem.txt') Result: ('/home/swaroop/byte/code', 'poem.txt')

Separation Extension: os.path.splitext()

Get the path name: os.path.dirname()

Get the file name: os.path.basename()

Run the shell command: os.system()

Read and set environment variables: os.getenv() and os.putenv()

give the line terminator used by the current platform: os .linesep Windows uses '\r\n', Linux uses '\n' and Mac uses '\r'

Indicate which platform you are using: os.name For Windows it is 'nt' while For Linux/Unix users it is 'posix'

Rename: os.rename(old, new)

Create multi-level directories: os.makedirs(r"c:\python\ test")

Create a single directory: os.mkdir("test")

Get file attributes: os.stat(file)

Modify file permissions and timestamps: os.chmod(file)

Terminate the current process: os.exit()

Get the file size: os.path.getsize(filename)


File operations :
os.mknod("test.txt") creates an empty file
fp = open("test.txt",w) directly opens a file, and creates the file if it does not exist

About open mode:

w opens for writing,
a opens in append mode (starts from EOF, creates a new file if necessary)
r+ opens in read-write mode
w+ opens for reading Open in 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 Open (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]) #Read a line. If size is defined, it may be returned. Just a part of a line

fp.readlines([size]) # Treat each line of the file as a member of a list and return the 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 to the file, write() will not add a newline character after str

fp.writelines(seq) #Write seq All contents are written to the file (multiple lines are 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 be generated

fp.flush() #Write the contents of the buffer to the hard disk

fp.fileno() #Return a long integer Type of "file label"

fp.isatty() #Whether the file is a terminal device file (in a unix system)

fp.tell() #Return the current position of the file operation mark , taking the beginning of the file as 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 calculation from the beginning, and 1 to calculate from 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.

Directory operation:
os.mkdir("file") Create directory
Copy 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
Copy folder:
shutil.copytree("olddir","newdir ") Both olddir and newdir can only be directories, and newdir must not exist
Rename files (directories)
os.rename("oldname","newname") Use this command for files or directories
Move files (directories)
shutil.move("oldpos","newpos")
Delete files
os.remove("file")
Delete directories
os.rmdir( "dir") can only delete empty directories
shutil.rmtree("dir") Both empty directories and directories with content can be deleted
Convert directory
os.chdir("path") Change path

Python reads and writes files

1.open
After using open to open a file, you must remember to call the close() method of the file object. For example, you can use the try/finally statement to ensure that the file can be closed finally.

file_object = open('thefile.txt')
try:
all_the_text = file_object.read( )
finally:
file_object.close( )

Note: The open statement cannot be placed in the try block because when an exception occurs when opening the file, the file object file_object cannot execute the close() method.

2. Read files
Read text files
input = open('data', 'r')
#The second parameter defaults to r
input = open(' data')

Read binary file
input = open('data', 'rb')

Read all contents
file_object = open('thefile. txt')
try:
all_the_text = file_object.read( )
finally:
file_object.close( )

Read fixed bytes
file_object = open ('abinfile', 'rb')
try:
while True:
chunk = file_object.read(100)
if not chunk:
break
do_something_with(chunk)
finally:
file_object.close( )

Read each line
list_of_all_the_lines = file_object.readlines( )

If the file is a text file, you can also traverse it directly File object gets each line:

for line in file_object:
process line

3. Write file
Write text file
output = open('data ', 'w')

Write binary file
output = open('data', 'wb')

Append write file
output = open('data', 'w+')

Write data
file_object = open('thefile.txt', 'w')
file_object.write(all_the_text)
file_object.close( )

Write multiple lines
file_object.writelines(list_of_text_strings)

Note that calling writelines to write multiple lines is more efficient than using write at once Write to high.

When processing log files, we often encounter such a situation: the log file is huge and it is impossible to read the entire file into the memory for processing at one time. For example, it needs to be on a computer with a physical memory of 2GB. Processing a 2GB log file on the machine, we may want to process only 200MB of its content at a time.
In Python, the built-in File object directly provides a readlines(sizehint) function to accomplish such a thing. Take the following code as an example:

file = open('test.log', 'r')sizehint = 209715200 # 200Mposition = 0lines = file.readlines(sizehint)while not file.tell() - position < 0: position = file.tell() lines = file.readlines(sizehint)

Every time the readlines(sizehint) function is called, approximately 200MB of data will be returned, and the data returned must be complete. Line data, in most cases, the number of bytes of the returned data will be slightly larger than the value specified by sizehint (except when the readlines(sizehint) function is called for the last time). Normally, Python will automatically adjust the user-specified sizehint value to an integer multiple of the internal cache size.

file is a special type in python, which is used to operate external files in python programs. Everything in Python is an object, and file is no exception. File has file methods and attributes. Let’s first look at how to create a file object:


file(name[, mode[, buffering]])
The file() function is used to create a file object. It has an alias called open(), which may be more vivid. They are built-in functions. . Let’s take a look at its parameters. Its parameters are all passed in the form of strings. name is the name of the file.
mode is the open mode. The optional values ​​are r w a U, which represent read (default) and write. Add modes that support various line breaks. If you open a file in w or a mode, if the file does not exist, it will be created automatically. In addition, when using w mode to open an existing file, the content of the original file will be cleared, because the initial file operation mark is at the beginning of the file. If you perform a write operation at this time, the original content will undoubtedly be deleted. Erase it. Due to historical reasons, the newline character has different modes in different systems. For example, in Unix it is a \n, but in Windows it is '\r\n'. Opening a file in U mode supports all newline modes. In other words, '\r' '\n' '\r\n' can represent a newline, and there will be a tuple used to store the newline characters used in this file. However, although there are many modes for line breaks, when reading python, they are always replaced by \n. After the mode character, you can also add the two signs + b t, which respectively indicate that the file can be read and written at the same time and the file can be opened in binary mode or text mode (default).
buffering If it is 0, it means no buffering; if it is 1, it means "line buffering"; if it is a number greater than 1, it means the size of the buffer, which should be in bytes.

The file object has its own properties and methods. Let’s first look at the attributes of file.


closed #Mark whether the file has been closed, rewritten by close()
encoding #File encoding
mode #Open mode
name #File name
newlines #In the file The newline mode used is a tuple
softspace #boolean type, usually 0. It is said to be used for reading and writing print

file:

F.read([size] ) #size is the length of the read, in bytes.
F.readline([size])
#Read a line. If size is defined, it is possible to return only a part of the line.
F.readlines ([size])
# Treat each line of the file as a member of a list and return the 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.
F.write(str)
#Write str to the file, write() will not add a newline character after str
F.writelines(seq)
#Put seq All contents are written to the file. This function also just writes faithfully, without adding anything after each line.

Other methods of file:

F.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 be generated
F.flush()
#Write the contents of the buffer to the hard disk
F.fileno()
#Return a long integer Type of "file label"
F.isatty()
#Whether the file is a terminal device file (in a unix system)
F.tell()
#Returns the current position of the file operation mark , taking the beginning of the file as the origin
F.next()
#Return to the next line and move the file operation flag 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.
F.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.
F.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.

The above method of reading, writing, and creating files in Python (a must-read) is all the content shared by the editor. I hope it can give you a reference, and I also hope that everyone will support the PHP Chinese website.

For more articles related to Python methods of reading, writing, and creating files, please pay attention to the PHP Chinese website!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP and Python: Different Paradigms Explained PHP and Python: Different Paradigms Explained Apr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

Python vs. JavaScript: The Learning Curve and Ease of Use Python vs. JavaScript: The Learning Curve and Ease of Use Apr 16, 2025 am 12:12 AM

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

Choosing Between PHP and Python: A Guide Choosing Between PHP and Python: A Guide Apr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

Can vs code run in Windows 8 Can vs code run in Windows 8 Apr 15, 2025 pm 07:24 PM

VS Code can run on Windows 8, but the experience may not be great. First make sure the system has been updated to the latest patch, then download the VS Code installation package that matches the system architecture and install it as prompted. After installation, be aware that some extensions may be incompatible with Windows 8 and need to look for alternative extensions or use newer Windows systems in a virtual machine. Install the necessary extensions to check whether they work properly. Although VS Code is feasible on Windows 8, it is recommended to upgrade to a newer Windows system for a better development experience and security.

Can visual studio code be used in python Can visual studio code be used in python Apr 15, 2025 pm 08:18 PM

VS Code can be used to write Python and provides many features that make it an ideal tool for developing Python applications. It allows users to: install Python extensions to get functions such as code completion, syntax highlighting, and debugging. Use the debugger to track code step by step, find and fix errors. Integrate Git for version control. Use code formatting tools to maintain code consistency. Use the Linting tool to spot potential problems ahead of time.

PHP and Python: A Deep Dive into Their History PHP and Python: A Deep Dive into Their History Apr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

How to run programs in terminal vscode How to run programs in terminal vscode Apr 15, 2025 pm 06:42 PM

In VS Code, you can run the program in the terminal through the following steps: Prepare the code and open the integrated terminal to ensure that the code directory is consistent with the terminal working directory. Select the run command according to the programming language (such as Python's python your_file_name.py) to check whether it runs successfully and resolve errors. Use the debugger to improve debugging efficiency.

Is the vscode extension malicious? Is the vscode extension malicious? Apr 15, 2025 pm 07:57 PM

VS Code extensions pose malicious risks, such as hiding malicious code, exploiting vulnerabilities, and masturbating as legitimate extensions. Methods to identify malicious extensions include: checking publishers, reading comments, checking code, and installing with caution. Security measures also include: security awareness, good habits, regular updates and antivirus software.

See all articles