Operate on files and directories in Python

File and directory operations is a programming language very important function, Python also offers its corresponding API support, this blog is to introduce specific methods of their use.

First, the file open and close operations

The file operations are divided into three steps in Python, first to open the file, then the file read and write operations, and finally need to close the file.

1, is used to open the file open () function , which provides initialization input and output (I / O) operations common interface, returns a file object file after a successful open, an error is thrown open failure. Open the file has the following syntax:

file_object = open(file_name,access_mode [,buffering] )

file_name is to open the file name, file path can be absolute or relative path. Absolute path of the file on the hard drive really exist path, such as: c: \ python \ src is an absolute path. The relative path is relative to the currently running program file location where the target, indicates the relative path, indicating the current position "."; ".." indicates the current position of a. Such as ".. \ images" or ". \ DB" is a relative path, the benefits of using a relative path, when to migrate, because the absolute path closely associated with the local computer, run the program might be wrong, so usually use relative paths.

access_mode represented file should be opened, the common mode is as follows:

Operate on files and directories in Python

r represents the file is a read operation, data is written to a file represents W, a is appended to the end of the data file, b is an ID file is a binary file, used in combination with RWA, such as opening a binary file required audio and video b use mode, the default mode is not the specified pattern r.

buffering denotes a buffer access to the file adopted. 0 means no buffer, 1 indicates that only one line buffer, any value greater than 1 indicates a given value as buffer size, this parameter is not provided or given negative indication system default buffer mechanism.

(2) the file read and write operations need to close the file , the file object is occupied resources are released, using File.close () method, file represented by file object is opened. If you do not explicitly close the file, Python's garbage collection mechanism will be in the file object's reference count is zero automatically closes the file, but you may lose data output buffer. If you do not close the file has been opened, the resource file will be occupied, you can not perform other operations such as deleting files to the file. Therefore, to develop good habits, after the completion of file operations to close the file, the release of resources.

(3) to open and close files sample code is as follows:

fp = open('e:/readme.txt','w')       #绝对路径写文件
fp.close()           #关闭文件

fp = open('./readme.txt','r')          #相对路径,读文件
fp.close()

The first is to use absolute paths, "w" represents the file read and write operations, if the file does not exist it will create an empty file. The second is to use relative paths, look in the directory of the current Python file, "r" for read, if no error occurs, find the file will be read.

(4) built-in methods commonly used file object

Operating method of file objects have it right, commonly used methods are summarized as follows:
Operate on files and directories in Python

(5) Operation Contents

1) may be used getcwd () function to get the current path.

#代码如下:
import os
print os.getcwd()
#执行结果如下:
C:\Users\Administrator\Desktop          #因为我Python的程序在桌面,所以显示的是这个路径

2) Use listdir (path) can get a directory listing of all files in the directory below.

#代码如下:
import os
print os.listdir(os.getcwd())
#执行结果如下:
['aa.py', 'APP', 'desktop.ini', 'files']

3) Create a new directory using mkdir (path) function.

#代码如下:
import os
print os.mkdir('test')
#执行结果就是在当前路径下新建了一个目录,也可以指定全路径进行创建

When you delete a directory, as long as the mkdir rmdir can be replaced.

4) determines whether a directory exists using path.isdir (path).

#代码如下
import os
print os.path.isdir('test')
#执行后,如果目录存在,则返回True,若不存在,则返回False。

Determine whether the file can be used path.isfile (path).

#代码如下
import os
print os.path.isfile('test')

-------- end of this article so far, thanks for reading --------

Guess you like

Origin blog.51cto.com/14154700/2440347