Python File Operations Summary

Source: Internet
Author: User
Tags truncated

Python file Operations Summary file operations Close () overview

The close () method is used to close an open file. The closed file can no longer read or write, or it will trigger a valueerror error. The close () method allows calls multiple times.
When the file object is referenced to manipulate another file, Python automatically shuts down the file object before it. Using the Close () method to close a file is a good habit.

Grammar

The close () method syntax is as follows:
Fileobject.close ();

Parameters

No

return value

The method has no return value.

Flush () Overview

The flush () method is used to flush the buffer, the data in the buffer is immediately written to the file, and the buffer is emptied, without the need to wait for the output buffer to write.
In general, the buffer is refreshed automatically when the file is closed, but sometimes you need to refresh it before closing, so you can use the flush () method.

Grammar

The flush () method syntax is as follows:

fileObject.flush();
Parameters

No

return value

The method has no return value.

Fileno () overview

The Fileno () method returns a file descriptor for an integral type (a descriptor FD integer) that can be used for I/O operations of the underlying operating system.

Grammar

The Fileno () method syntax is as follows:

Parameters

No

return value

Returns the file descriptor.

Isatty () Method overview

The Isatty () method detects if the file is connected to an end device and returns False if True.

Grammar

The Isatty () method syntax is as follows:

Parameters

No

return value

Returns True if connected to an end device, otherwise False is returned.

Next () overview

The next () method is used when the file uses an iterator, and in the loop, the next () method is called in each loop, which returns the next line of the file and, if it reaches the end (EOF), triggers the stopiteration

Grammar

The next () method syntax is as follows:

Parameters

No

return value

Returns the next line of the file.

Read () overview

The read () method is used to read the specified number of bytes from the file, and all if not given or negative.

Grammar

The read () method syntax is as follows:

Parameters

size– the number of bytes read from the file.

return value

Returns the bytes read from the string.

ReadLine () overview

The ReadLine () method is used to read an entire line from a file, including the "\ n" character. If a non-negative parameter is specified, the number of bytes of the specified size is returned, including the "\ n" character.

Grammar

The ReadLine () method syntax is as follows:

Parameters

size– the number of bytes read from the file.

return value

Returns the bytes read from the string.

ReadLines () overview

The ReadLines () method reads all rows (until the end of EOF) and returns a list, and if given sizeint>0, returns a row with a sum of approximately sizeint bytes, the actual read value may be larger than sizhint because the buffer needs to be populated.
Returns an empty string if the Terminator EOF is encountered.

Grammar

The ReadLines () method syntax is as follows:

fileObject.readlines( sizehint );
Parameters

sizehint– the number of bytes read from the file.

return value

Returns a list that contains all the rows.

Seek () overview

The Seek () method is used to move the file reading pointer to the specified location.

Grammar

The Seek () method syntax is as follows:

fileObject.seek(offset[, whence])
Parameters

offset– the starting offset, which is the number of bytes needed to move the offset
Whence: Optional, default value is 0. Give the offset parameter a definition of where to start the offset, and 0 to start at the beginning of the file, 1 to start at the current position, and 2 for the end of the file.

return value

The function does not return a value.

#!/usr/bin/python# -*- coding: UTF-8 -*-# 打开文件fo = open("runoob.txt""rw+")print"文件名为: ", fo.nameline = fo.readline()print"读取的数据为: %s" % (line)# 重新设置文件读取指针到开头fo.seek(00)line = fo.readline()print"读取的数据为: %s" % (line)# 关闭文件fo.close()
Tell () overview

The Tell () method returns the current position of the file, which is the current position of the file pointer.

Grammar

The tell () method syntax is as follows:

fileObject.tell(offset[, whence])
Parameters

No

return value

Returns the current location of the file.

Truncate () overview

The truncate () method is used to truncate a file, and if an optional parameter size is specified, the truncated file is a size character. If size is not specified, it is truncated from the current position, and all characters after the size are deleted after truncation.

Grammar

The truncate () method syntax is as follows:

fileObject.truncate( [ size ])
Parameters

size– Optional, if present, the file is truncated to size bytes.

return value

The method has no return value.

Write () overview

The Write () method is used to write the specified string to the file.
The contents of the string are stored in the buffer before the file is closed or before the buffer is flushed, and you cannot see what is written in the file.

Grammar

The write () method syntax is as follows:

fileObject.write( [ str ])
Parameters

str– the string to write to the file.

return value

The method has no return value.

Writelines () overview

The Writelines () method is used to write a sequence of strings to a file.
This sequence string can be generated by an iterative object, such as a list of strings.
Line breaks need to be made with a newline character \ n.

Grammar

The Writelines () method syntax is as follows:
Fileobject.writelines ([str])

Parameters

str– the string sequence to write to the file.

return value

The method has no return value.

OS system commands

Python OS File/directory method

Python can call system commands directly, this method is very useful!

osos.chdir("EdenTestData")os.chdir("400")os.system("g++ main.cpp")os.system("./a.out")

Python File Operations Summary

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.