11 Python file operations,

Source: Internet
Author: User

11 Python file operations,
File Operation Procedure

Computer systems are divided into three parts: computer hardware, operating system, and applications.

Applications written in python or other languages must be stored on the hard disk if they want to permanently Save the data. This involves hardware operations. As we all know, applications cannot directly operate on the hardware, so the operating system is used. The operating system encapsulates complex hardware operations into simple interfaces for users/applications. The files are virtual concepts that the operating system provides to applications to operate on hard disks, users or applications can save their data permanently by operating on files.

With the concept of files, we don't have to worry about the details of operating the hard disk. We just need to pay attention to the file operation process:

1. open the file, get the file handle, and assign it to a variable. 2. Operate the file through the handle. 3. close the file.
1 #1. open the file, get the file handle, and assign a value to the variable 2 f1_open('a.txt ', 'R', encoding = 'utf-8') # The default open mode is r3 4 #2. use a handle to operate the file. 5 data = f. read () 6 7 #3. close file 8 f. close ()
Coding
1. The application initiates a system call open (...) to the operating system (...) 2. The operating system opens the file and returns a file handle to the application. 3. The application assigns the file handle to the variable f.

Precautions for closing files

1. Opening a file contains two resources: the file opened at the operating system level + the variable of the application. After a file is operated, the two resources of the file must not be recycled. The recovery method is: 2 1. f. close () # reclaim files opened at the operating system level 3 2. del f # reclaim application-level variables 4 5 where del f must occur in f. after close (), otherwise, the files opened by the operating system are not closed, and resources are occupied in vain. 6. The python automatic garbage collection mechanism determines that we do not need to consider del f, this requires us to remember f. close () Although I said so, many people will still forget f. close (), we recommend that you use the with keyword to manage the Context 9 with open('a.txt ', 'w') as f: 10 pass11 12 with open('a.txt ', 'R') as read_f, open(' B .txt', 'w') as write_f: 13 data = read_f.read () 14 write_f.write (data)
View Code file encoding

F = open (...) the operating system opens the file. If we do not specify the encoding for open, the default encoding for opening the file is clearly determined by the operating system, the operating system uses its own default encoding to open the file, which is gbk in windows and UTF-8 in linux.

1 # This applies to the character encoding Knowledge mentioned in the previous lesson: to ensure no garbled characters, how to store files should be opened. 2 f=open('a.txt ', 'R', encoding = 'utf-8 ')
View Code file opening Mode
File handle = open ('file path', 'Mode ')

The mode can be a combination of the following methods:

Character Meaning
'R' Open for reading (default)
'W' Open for writing, truncating the file first
'A' Open for writing, appending to the end of the file if it exists
'B' Binary mode
'T' Text mode (default)
'+' Open a disk file for updating (reading and writing)
'U' Universal newline mode (for backwards compatibility; shocould not be used in new code)

1. the file opening mode is available (the default mode is text): r, read-only mode [default mode, the file must exist, and an exception is thrown if the file does not exist] w, write-only mode [unreadable; if it does not exist, it is created. If it exists, the content is cleared.] a, the append write mode [unreadable; if it does not exist, the content is created. If it exists, only the content is appended.] 2. for non-text files, we can only use the B mode. "B" indicates that operations are performed in bytes (and all files are stored in bytes, in this mode, you do not need to consider the character encoding of text files, the jgp format of image files, and the avi format of video files.) rb wb AB Note: When you open a file in the B mode, the read content is of the byte type. You must provide the byte type when writing data. Encoding cannot be specified.
3. "+" indicates that you can read and write a file r + at the same time, read and write [readable, writable] a +, write and read [readable, writable] x, write-only mode: [unreadable; creation if no data exists. If yes, an error is returned.] x +, write-read [readable, writable] xb
For historical reasons, line breaks have different modes in different systems. For example, in unix, the line breaks are \ n, and in windows, the line breaks are \ r \ n ', opening a file in U mode supports all line breaks, that is to say, '\ r'' \ n' \ r \ n' can indicate that t is the so-called text mode (text mode) exclusive to the windows platform ), the difference is that it automatically recognizes line breaks on windows.
Files opened in binary mode (appending 'B' to the mode argument) return contents as bytes objects without any decoding.
B reads a file in binary format, but does not display 0101, but in bytes. One byte is an 8-bit binary, so the computer automatically converts it. Do not misunderstand that the B mode reads data by byte.

 

Move the cursor inside the file 1: read (3 ):

1. When the file is opened in text mode, it indicates that three characters are read.

2. When the file is opened in B mode, three bytes are read.

Ii. move the cursor in other files in bytes, such as seek, tell, and truncate.

Note:

1. There are three ways to move seek: 0, 1, 2, where 1 and 2 must be in B mode, but either mode moves in bytes

2. truncate truncates a file. Therefore, the file must be writable, but cannot be opened by w or w +, because the file is cleared directly, therefore, truncate must test the effect in r +, a, or a + mode.

With context management

Opening a file contains two resources: file opened at the operating system level + Application variables. After a file is operated, the two resources of the file must not be recycled. The recovery method is as follows:
1. f. close () recycles files opened by the operating system.
2. del f recycles application-level variables

1 with open('a.txt','w') as f:2     pass3  4 with open('a.txt','r') as read_f,open('b.txt','w') as write_f:5     data=read_f.read()6     write_f.write(data)
Modify the View Code file

The file data is stored on the hard disk, so there is only one overwrite, there is no such change, we usually see the modified files, are simulated results, specifically, there are two implementation methods:

Method 1: load all the content of the file stored in the hard disk to the memory, which can be modified in the memory. After modification, the memory overwrites the hard disk (word, vim, nodpad ++ editor)

1 import OS 2 3 with open('a.txt ') as read_f,open('.a.txt. swap ', 'w') as write_f: 4 data = read_f.read () # Read all data into the memory. If the file is large, 5 data = data will be stuck. replace ('Alex ', 'SB') # modify 6 7 write_f.write (data) in the memory # write a new file 8 9 OS .remove('a.txt ') 10 OS .rename('.a.txt.swap', 'a.txt ')
View Code

Method 2: Read the content of the file stored on the hard disk One by one into the memory, write the content to the new file after modification, and overwrite the source file with the new file.

1 import os2 3 with open('a.txt') as read_f,open('.a.txt.swap','w') as write_f:4     for line in read_f:5         line=line.replace('alex','SB')6         write_f.write(line)7 8 os.remove('a.txt')9 os.rename('.a.txt.swap','a.txt') 
View Code

Files and folders in python(File operation functions)The operation involves the OS module and shutil module.

Obtain the current working directory, that is, the directory path of the current Python script:OS. getcwd ()

Returns the names of all objects and directories in the specified directory:OS. listdir ()

The function is used to delete an object:OS. remove ()

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

Check whether the given path is a file:OS. path. isfile ()

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

Determine whether it is an absolute path:OS. path. isabs ()

Check whether the given path is actually saved:OS. path. exists ()

Returns 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 ()

Obtain the path name:OS. path. dirname ()

Get File Name:OS. path. basename ()

Run the shell command:OS. system ()

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

The line terminator used by the current platform is provided:OS. linesepIn Windows, '\ r \ n' is used, in Linux,' \ n' is used, and in Mac, '\ R' is used'

Indicates the platform you are using:OS. nameFor Windows, it is 'nt ', and for Linux/Unix users, it is 'posix'

Rename:OS. rename (old, new)

Create a multilevel directory:OS. makedirs (r "c: \ python \ test ")

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

Get file attributes:OS. stat (file)

Modify the File Permission and timestamp:OS. chmod (file)

Terminate the current process:OS. exit ()

Get file size:OS. path. getsize (filename)


File Operations:
OS. mknod ("test.txt ")Create an empty file
Fp = open ("test.txt", w)Open a file directly. If the file does not exist, create the file.

About open mode:

W open in write mode,
A is opened in append mode (starting from EOF and creating a file if necessary)
R + Enabled in read/write mode
W + is enabled in read/write mode (see w)
A + is enabled in read/write mode (see)
Rb is enabled in binary read mode.
Wb is enabled in binary write mode (see w)
AB is enabled in binary append mode (see)
Rb + is enabled in binary read/write mode (see r +)
Wb + is enabled in binary read/write mode (see w +)
AB + is enabled 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 a file. write () does not add a linefeed after str.

Fp. writelines (seq)# Write All seq content to the 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. fileno ()# Return a long integer "file tag"

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

Fp. tell ()# Return the current position of the file operation mark, starting with the file

Fp. next ()# Return the next row and move the operation mark of the file to the next row. Use a file... When a statement such as in file is called, the next () function is called to implement traversal.

Fp. seek (offset [, whence])# Move the file to the offset position by marking the operation. 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 a specified size. The default value is to crop it to the position 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.

 

Directory operation:
OS. mkdir ("file ")Create directory
Copy a 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 a file (directory)
OS. rename ("oldname", "newname ")This command is used for files or directories.
Move a file (directory)
Shutil. move ("oldpos", "newpos ")
Delete an object
OS. remove ("file ")
Delete directory
OS. rmdir ("dir ")Only empty directories can be deleted.
Shutil. rmtree ("dir ")You can delete empty directories and directories with content.
Conversion directory
OS. chdir ("path ")Change path

  1Add '_ fc' to the names of all images in the folder'

1 #-*-coding: UTF-8-*-2 import re 3 import OS 4 import time 5 # str. split (string) Splits string 6 # 'connector '. join (list) combines the list into a string 7 def change_name (path): 8 global I 9 if not OS. path. isdir (path) and not OS. path. isfile (path): 10 return False11 if OS. path. isfile (path): 12 file_path = OS. path. split (path) # split the Directory and file 13 lists = file_path [1]. split ('. ') # split the file and file extension 14 file_ext = lists [-1] # retrieve the suffix (list slicing operation) 15 img_ext = ['bmp', 'jpeg ', 'gif', 'psd ', 'png', 'jpg '] 16 if file_ext in img_ext: 17 OS. rename (path, file_path [0] + '/' + lists [0] + '_ fc. '+ file_ext) 18 I + = 1 # note that here I is a trap 19 # Or 20 # img_ext = 'bmp | jpeg | gif | psd | png | jpg '21 # if file_ext in img_ext: 22 # print ('OK ---' + file_ext) 23 elif OS. path. isdir (path): 24 for x in OS. listdir (path): 25 change_name (OS. path. join (path, x) # OS. path. join () is useful in path processing 26 27 28 img_dir = 'd: \ xx \ images '29 img_dir = img_dir.replace ('\\', '/') 30 start = time. time () 31 I = 032 change_name (img_dir) 33 c = time. time ()-start34 print ('program running time: % 0.2f '% (c) 35 print ('% s images processed in total' % (I )) 36 37 output result: 38 39 program running time: 0.1140 109 images processed in total
View Code

 

Related Article

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.