Python File Operations Summary

Source: Internet
Author: User
Tags create directory unpack

The importance of file manipulation to programming languages is needless to say, if data cannot be persisted, information technology loses its meaning. According to my experience, Io is also a pretty headache, because it will not be used too much, so always remember the API, each time to re-Google will interrupt the idea, not necessarily every time to get the right article.

The contents of this article include:

File read and write operations

Various system operations for files

Storage objects

Character-based Read & write

The most basic file operation is of course to read and write data in the file. This is also very easy to grasp. Now open a file for the write operation:

1. FileHandle = open (' Test.txt ', ' W ')

FileHandle = open (' Test.txt ', ' W ')

' W ' means that the file will be written to the data, and the other parts of the statement are well understood. The next step is to write the data to the file:

1. Filehandle.write (' This was a test.\nreally, it is. ')

Filehandle.write (' This was a test.\nreally, it is. ')

This statement will "this is a test." Write the first line of the file, "really, it is." Writes the second line of the file. Finally, we need to do cleanup work and close the file:

1. Filehandle.close ()

Filehandle.close ()

As you can see, this is really very simple under the object-oriented mechanism of Python. Note that when you use the "W" method to write data in a file again, all the original content is deleted. If you want to keep the original content, you can use the "a" method to append the data to the end of the file:

1. FileHandle = open (' Test.txt ', ' a ')
2. Filehandle.write (' \n\nbottom line. ')
3. Filehandle.close ()

FileHandle = open (' Test.txt ', ' a ')
Filehandle.write (' \n\nbottom line. ')
Filehandle.close ()

We then read the Test.txt and display the contents:

1. FileHandle = open (' Test.txt ')
2. Print Filehandle.read ()
3. Filehandle.close ()

FileHandle = open (' Test.txt ')
Print Filehandle.read ()
Filehandle.close ()

The above statement reads the entire file and displays the data in it.

Row-based read-write line

1. FileHandle = open (' Test.txt ')
2. Print filehandle.readline () # "This is a test."
3. Filehandle.close ()

FileHandle = open (' Test.txt ')
Print filehandle.readline () # "This is a test."
Filehandle.close ()


You can also save the contents of a file to a list:

1. FileHandle = open (' Test.txt ')
2. FileList = Filehandle.readlines ()
3. For fileline in FileList:
4. print ' >> ', fileline
5. Filehandle.close ()

FileHandle = open (' Test.txt ')
FileList = Filehandle.readlines ()
For Fileline in FileList:
print ' >> ', fileline
Filehandle.close ()

Or read the contents of a few bytes at a time in a file:

1. FileHandle = open (' Test.txt ')
2. Print Filehandle.read (1) # "T"
3. Filehandle.seek (4)
4. Print Filehandle.read (1) # "" (Original error)

FileHandle = open (' Test.txt ')
Print Filehandle.read (1) # "T"
Filehandle.seek (4)
Print Filehandle.read (1) # "" (Original error)

Random access to the location in the file seek

When you read a file, Python remembers its location in the file, as follows:

1. FileHandle = open (' Test.txt ')
2. Garbage = Filehandle.readline ()
3. Filehandle.readline () # "Really, it is." Filehandle.close ()

FileHandle = open (' Test.txt ')
garbage = Filehandle.readline ()
Filehandle.readline () # "Really, it is." Filehandle.close ()

As you can see, only the second line shows up. However, we can let Python read from scratch to solve this problem:

1. FileHandle = open (' Test.txt ')
2. Garbage = Filehandle.readline ()
3. Filehandle.seek (0)
4. Print filehandle.readline () # "This is a test."
5. Filehandle.close ()

FileHandle = open (' Test.txt ')
garbage = Filehandle.readline ()
Filehandle.seek (0)
Print filehandle.readline () # "This is a test."
Filehandle.close ()

In the above example, we let Python start reading data from the first byte of the file. So, the first line of text shows up. Of course, we can also get the location of Python in the file:

1. FileHandle = open (' Test.txt ')
2. Print filehandle.readline () # "This is a test."
3. Print Filehandle.tell () # "17"
4. Print filehandle.readline () # "Really, it is."

FileHandle = open (' Test.txt ')
Print filehandle.readline () # "This is a test."
Print Filehandle.tell () # "17"
Print filehandle.readline () # "Really, it is."

Binary mode Read and write

In Windows and Macintosh environments, it may sometimes be necessary to read and write files, tablets, and executables in binary mode. At this point, simply add a "B" to the mode parameter of the open file:

1. FileHandle = open (' TestBinary.txt ', ' WB ')
2. Filehandle.write (' There is no spoon. ')
3. Filehandle.close ()

FileHandle = open (' TestBinary.txt ', ' WB ')
Filehandle.write (' There is no spoon. ')
Filehandle.close ()

1. FileHandle = open (' TestBinary.txt ', ' RB ')
2. Print Filehandle.read ()
3. Filehandle.close ()

FileHandle = open (' TestBinary.txt ', ' RB ')
Print Filehandle.read ()
Filehandle.close ()

python itself does not support binary , but it provides a module to compensate for it, which is the struct module.

Python does not have a binary type, but it can store binary types of data, that is, using string string types to store binary data, which is fine because the string is in 1 bytes.

Import struct

a=12.34

#将a变为二进制

Bytes=struct.pack (' I ', a)

At this point, Bytes is a string literal, which is the same as the binary storage of a byte in bytes.

And then reverse the operation.

The existing binary data bytes, which is actually a string, translates it into a Python data type:

A,=struct.unpack (' i ', bytes)

Note that the unpack returns a tuple

So if there is only one variable:

Bytes=struct.pack (' I ', a)

Well, that's what it takes to decode.

A,=struct.unpack (' i ', bytes) or (A,) =struct.unpack (' I ', bytes)

If you use A=struct.unpack directly (' I ', bytes), then a= (12.34,) is a tuple instead of the original floating-point number.

If it is composed of multiple data, you can:

A= ' Hello '

b= ' world! '

c=2

D=45.123

Bytes=struct.pack (' 5s6sif ', a,b,c,d)

At this point the bytes is the binary form of the data, you can write directly to the file such as Binfile.write (bytes)

Then, when we need to, we can read it again, Bytes=binfile.read ()

Then decode the python variable by struct.unpack ()
A,b,c,d=struct.unpack (' 5s6sif ', bytes)

' 5s6sif ' is called FMT, which is a formatted string, consisting of numbers plus characters, 5s representing a 5-character string, 2i, representing 2 integers, and so on, the following are the available characters and types, and the CType representation can correspond to type one by one in Python.

===============================================================================================

Various system operations

The operation of files, folders (file manipulation functions) in Python involves both the OS module and the Shutil module.

Get the current working directory, that is, the directory path of the current Python script work: OS.GETCWD ()

Returns all files and directory names under the specified directory name: Os.listdir ()

function to delete a file: Os.remove ()

Delete multiple directories: Os.removedirs (r "C:\python")

Verifies whether the given path is a file: Os.path.isfile ()

Verify that the given path is a directory: Os.path.isdir ()

Determine if it is an absolute path: Os.path.isabs ()

Check if shortcut os.path.islink (filename)

Verify that the given path is really 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 ')

Detach extension: Os.path.splitext ()

Get path name: Os.path.dirname ()

Get file name: Os.path.basename ()

Run shell command: Os.system ()

Read and SET Environment variables: os.getenv () and os.putenv ()

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

Indicates the platform you are using: Os.name for 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 file permissions and timestamps: Os.chmod (file)

Terminate current process: Os.exit ()

Get File Size: os.path.getsize (filename)


File operation:
Os.mknod ("test.txt") Create an empty file
fp = open ("Test.txt", W) opens a file directly and creates a file if the file does not exist

About open mode:

W opens in write mode,
A opens in Append mode (starting with EOF and creating a new file if necessary)
R+ Open in read-write mode
w+ Open in read/write mode (see W)
A + opens in read/write mode (see a)
RB opens in binary read mode
WB opens in binary write mode (see W)
AB opens in binary append mode (see a)
Rb+ opens in binary read/write mode (see r+)
Wb+ opens in binary read/write mode (see w+)
Ab+ opens in binary read/write mode (see A +)

Fp.read ([size]) #size为读取的长度, in bytes

Fp.readline ([size]) #读一行, if size is defined, it is possible to return only part of a row

Fp.readlines ([size]) #把文件每一行作为一个list的一个成员 and returns the list. In fact, its internal is through the Loop call ReadLine () to achieve. If you provide a size parameter, size is the total length of the read content, which means that it may be read only to a portion of the file.

Fp.write (str) #把str写到文件中, write () does not add a newline character after Str

Fp.writelines (seq) #把seq的内容全部写到文件中 (multi-line write-once). This function simply writes faithfully and does not add anything behind each line.

Fp.close () #关闭文件. Python will automatically close files after a file is not used, but this feature is not guaranteed and it is best to develop a habit of shutting them down. If a file is closed and then manipulated, it generates VALUEERROR

Fp.flush () #把缓冲区的内容写入硬盘

Fp.fileno () #返回一个长整型的 "file label"

Fp.isatty () #文件是否是一个终端设备文件 (on UNIX systems)

Fp.tell () #返回文件操作标记的当前位置, starting with the origin of the file

Fp.next () #返回下一行 and shifts the file action marker to the next line. When a file is used for a statement such as for ... in file, it is called the next () function to implement the traversal.

Fp.seek (Offset[,whence]) #将文件打操作标记移到offset的位置. 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, whence can be calculated from scratch for 0, and 1 for the current position as its origin. 2 means that the end of the file is calculated as the origin. Note that if the file is opened in a or a + mode, the file action tag is automatically returned to the end of the file each time the write operation is made.

Fp.truncate ([size]) #把文件裁成规定的大小, the default is the location that is cropped to the current file action tag. If the size of the file is larger, depending on the system may not change the file, it may be 0 files to the corresponding size, it may be some random content to add.

Directory Operations:
Os.mkdir ("file") Create directory
To copy a file:
Shutil.copyfile ("Oldfile", "NewFile") Oldfile and NewFile can only be files
Shutil.copy ("Oldfile", "NewFile") Oldfile can only be a folder, NewFile can be a file, or it can be a destination directory
To copy a folder:
Shutil.copytree ("Olddir", "Newdir") Olddir and Newdir can only be directories, and newdir must not exist
Renaming files (directories)
Os.rename ("Oldname", "newname") files or directories are all using this command
Moving Files (directories)
Shutil.move ("Oldpos", "Newpos")
deleting files
Os.remove ("file")
Delete Directory
Os.rmdir ("dir") can only delete empty directories
Shutil.rmtree ("dir") empty directory, contents of the directory can be deleted
Converting catalogs
Os.chdir ("path") Change path

PS: file operation, often with regular expressions :

Img_dir = img_dir.replace (' \ \ ', '/')

===============================================================================================

Storage objects

Using the modules described in the previous section, you can read and write strings in a file.
However, sometimes you may need to pass other types of data, such as list, tuple, dictionary, and other objects. In Python, you can use pickling to do this. You can use the "pickle" module in the Python standard library to complete the data grouping.
Next, let's group A list that contains strings and numbers:

1. Import Pickle
2.
3. FileHandle = open (' PickleFile.txt ', ' W ')
4. testlist = [' This ', 2, ' is ', 1, ' A ', 0, ' Test. ']
5. Pickle.dump (Testlist, FileHandle)
6. Filehandle.close ()

Import Pickle

FileHandle = open (' PickleFile.txt ', ' W ')
Testlist = [' This ', 2, ' is ', 1, ' A ', 0, ' Test. ']
Pickle.dump (Testlist, FileHandle)
Filehandle.close ()

Splitting a group is also not difficult:

1. Import Pickle
2.
3. FileHandle = open (' PickleFile.txt ')
4. testlist = Pickle.load (filehandle)
5. Filehandle.close ()

Import Pickle

FileHandle = open (' PickleFile.txt ')
Testlist = Pickle.load (filehandle)
Filehandle.close ()

Now try to store more complex data:

1. Import Pickle
2.
3. FileHandle = open (' PickleFile.txt ', ' W ')
4. testlist = [123, {' Calories ': [+], ' Mr Anderson ', [1, 2, 7]]
5. Pickle.dump (Testlist, FileHandle)
6. Filehandle.close ()

Import Pickle

FileHandle = open (' PickleFile.txt ', ' W ')
Testlist = [123, {' Calories ': ', ', ' Mr Anderson ', [1, 2, 7]]
Pickle.dump (Testlist, FileHandle)
Filehandle.close ()

1. Import Pickle
2.
3. FileHandle = open (' PickleFile.txt ')
4. testlist = Pickle.load (filehandle)
5. Filehandle.close ()

Import Pickle

FileHandle = open (' PickleFile.txt ')
Testlist = Pickle.load (filehandle)
Filehandle.close ()


As mentioned above, using Python's "pickle" module grouping is really simple. Many objects can be stored in a file by it. If possible, "Cpickle" is equally qualified for the job. It is the same as the "pickle" module, but faster:

1. Import Cpickle
2.
3. FileHandle = open (' PickleFile.txt ', ' W ')
4. Cpickle.dump (1776, FileHandle)
5. Filehandle.close ()

Import Cpickle

FileHandle = open (' PickleFile.txt ', ' W ')
Cpickle.dump (1776, FileHandle)
Filehandle.close ()

Python File Operations Summary

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.