This chapter describes the functions and types used in Python to read and write files and access directory content. Python provides a rich set of input and output functions, which are described in this chapter by using a wide range of functions, starting with the file object, which is the basic method for implementing input and output in Python, and then describe
If we were to execute directory and file operations in a Python program, the commands provided by the operating system simply invoked the interface functions provided by the operating system, and Python's built-in OS modules could invoke the interface functions provided by the operating system directly.To open the Python interactive command line, let's look at how to use the basic functionality of the OS mo
This article describes how Python traverses directories and replaces file names and directory names in batches. Share to everyone for your reference, specific as follows:
#encoding =utf-8 #author: Walker #date: 2014-03-07 #summary: Depth traverse the specified directory and change the subdirectory and file name to lowercase #注意, this program is only for Windows, Windows file (folder) name is case-inse
This example describes how Python reads all directories and files in a directory. Share to everyone for your reference, as follows:
Here is a new Python read the list of reading, to share the following:
#!/usr/bin/python#-*-Coding:utf8-*-import osallfilenum = 0def Printpath (level, PATH): Global Allfilenum "Prints all
Shutil modules: >>> Import shutil>>> shutil.copyfile (' test.txt ', ' 123.txt ') #参数只能是文件 >>> shutil.copy ("Olddir", " Newfileordir ") #olddir只能是文件夹, NewFile can be a file, or it can be a target directory >>> shutil.copytree (" Olddir "," Newdir ") # Olddir and Newdir can only be directories, and newdir must not exist >>> Shutil.move ("Oldpos", "Newpos") #移动文件或目录 >>> Shutil.rmtree (" Dir ") #空目录, content director
the user uses. In Python is the form of packages and modules.Libraries are generally designed according to API conventions.Application Interface (English:Application Programming Interface, abbreviation:API), also known as application programming interface is the agreement of the different components of the software system. Due to the increasing scale of software in recent years, it is often necessary to divide complex systems into small components, a
All files are contained in different directories, but Python is also easy to handle. The OS module has many ways to help you create, delete, and change directories.
mkdir () method
You can use the mkdir () method of the OS module to create new catalogs in the current directory. You need to provide a parameter that contains the name of the directory you want to c
This example describes how Python reads all directories and files in a directory. Share to everyone for your reference, specific as follows:
Here is a reading of the directory when just learning Python, to share the following:
#!/usr/bin/python #-*-Coding:utf8-*-import os allfilenum = 0 def printpath (level, p
the module to write a dir -l program that implements the output.
Write a program that finds the file name containing the specified string in the current directory and all subdirectories of the current directory, and prints a relative path.
fromDatetimeImportdatetimeImportospwd= Os.path.abspath ('.')Print('Size last Modified Name')Print('------------------------------------------------------------') forFinchOs.listdir (PWD): Fsize=os.path.getsize (f) mtime= Datetime.fromtimestamp (Os.p
os.remove (path) to delete the file path. If path is a directory, throw a oserror error. If you want to delete a directory, use rmdir (). Os.rmdir () can only delete empty directories Remove () is the same as unlink () in a Windows system, deleting a file that is in use will throw an exception. In Unix, the records in the catalog table are deleted, but the storage of the files is still there.os.removedirs (path) to delete the directory recursively.
This article describes the Python implementation method for deleting files and directories. Share to everyone for your reference. The implementation methods are as follows:
Os.remove (PATH)deletes the file path. If path is a directory, throw a oserror error. If you want to delete a directory, use RmDir ().
The remove () function is the same as unlink ()In a Windows system, deleting a file that is in use t
When you use Python to write a program, you often use a Third-party module package. We can import this package through the Python setup install after installation via import xxx or from xxx import yyy. However, if you are writing a dependency package, and do not want to install to the appropriate Python directory, you can put into this directory for import to cal
If we want to manipulate files and directories, we can enter various commands provided by the operating system under the command line to complete. such as dir , and cp other orders.What if you want to perform these directories and files in a python program? In fact, the commands provided by the operating system simply invoke the interface functions provided by th
File and directory operations: -File read and write :The function of Python for file reading and writing is open orFile_handler =Open (Filename,mode)Mode
Mode
Description
R
Read-only
r+
Write
W
Write, delete the original file before re-writing, if the file is not created
w+
Read and write, first delete the original file, re-write, if the file
# This is a learning note for the Liaoche teacher Python tutorial1. OverviewThe OS module can directly invoke the interface functions provided by the operating system. Help us manipulate directories and files in a Python program.part of the function for manipulating files and directories is placed in the OS module, a s
Import Osos.name # Operating system name Os.environ #版本 # to get the value of an environment variable, you can call the Os.getenv () function os.getenv (' PATH ')Manipulating Files and directories:Part of the function of manipulating files and directories is placed in the os module, part of the os.path module# View the absolute path of the current directory: Os.path.abspath ('. ') ' /users/michael ' # Create a new directory under a directory, # First,
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.