. The default file access method is read (r ).
Buffering: If the buffer value is set to 0, no buffer will occur. If the buffer value is 1, the row buffer accesses a file for execution. If the specified buffer value is an integer greater than 1, the buffer is used with the specified buffer size. If it is negative, the buffer size is the system default (default behavior ).
Here is a list of different modes for opening a file:
File object attributes:
Once a file is opened, the file object can obta
This article describes how to open a file in python and obtain file-related attributes. it involves Python file operations related skills, for more information about how to open a file in python and obtain the attributes of the file, see the following example. Share it with you for your reference. The specific analysis is as follows:
The following code uses the open function to open a file and outputs attributes such as the file name, open status, and open mode.
#! /Usr/bin/python # Open a
used. If the file does not exist, create a new file for reading and writing.
AB +
Open a file in binary format for append. If the file already exists, the file pointer is placed at the end of the file. If the file does not exist, create a new file for reading and writing.
File object attributesAfter a file is opened, you have a file object. You can obtain various information about the file.The following lists all attributes related to the file object
This example describes how Python opens a file and obtains file-related properties. Share to everyone for your reference. The specific analysis is as follows:
The following code opens the file through the open function and prints the properties such as file name, open State, open mode, and so on
#!/usr/bin/python
# Open a file
fo = open ("Foo.txt", "WB")
print "Name of the" file: ", fo.name
print" Clos Ed or not:, fo.closed
print "Opening mode:", Fo.mode
print "
Python to open a file and obtain file-related attributes,
This example describes how to open a file in python and obtain file-related attributes. Share it with you for your reference. The specific analysis is as follows:
The following code uses the open function to open a file and outputs attributes such as the file name, open status, and open mode.
#! /Usr/bin/python # Open a filefo = open ("foo.txt", "wb") print "Name of the file:", fo. nameprint "Closed or not:", fo. closedprint "Opening m
, file is a class that uses file (' file_name ', ' r+ ') to open files, return a filename object, open the file in write mode, and it will be created. But it is more recommended to use the built-in function open () to open a file, so let's take a look at the introduction to open ():
Help on built-in function open in module __builtin__:
Open (...)Open (name[, mode[, buffering])-> file objectThe Open a file using the file () type, returns a file object. This is thePreferred way to open a file. F
types of row Terminator, it is a list containing all the currently encountered rows.
File. softspace
If the value is 0, a space character is added after the output, and 1 indicates no. This attribute is generally not required by programmers and is used internally by the program.
Read () read (size) readline () readlines ()
In the previous example, the read () function is used to read all the content of a file at a time. If you ca
).
Here is a list of different modes for opening a file:
File Object properties:
Once the file is opened, the file object can get various information about the file.
The following is a list of all the properties related to the file object:
Example:
#!/usr/bin/python# Open a filefo = open ("Foo.txt", "WB") print "Name of the file:", Fo.nameprint "Closed or not:", fo.cl Osedprint "Opening mode:", Fo.modeprint "Softspace flag:", Fo.softspace
This
a tuple used to store the line breaks used in this file. However, although there are multiple line breaks, reading Python should be replaced by \ n. + B t can be added after the pattern character to indicate that the file can be read and written at the same time, and the file can be opened in binary and text modes by default.
If buffering is set to 0, no buffer is performed. If it is set to 1, "Row buffer" is performed. If it is a number greater than 1, the buffer size is measured in bytes.
Pyt
','__repr__','__setattr__','__sizeof__','__str__','__subclasshook__', 'Close', 'Closed', 'Encoding', 'Errors', 'Fileno', 'Flush', 'Isatty', 'Mode', 'Name', 'Newlines', 'Next', 'Read', 'Readinto', 'ReadLine', 'ReadLines','Seek','Softspace',' Tell','truncate','Write','Writelines','Xreadlines']>>>As a result, the code above can be simplified with the WITH statement, but the code is the same, but using the WITH statement is more concise:With open ("log.tx
, create a new file for reading and writing.
File object attributesAfter a file is opened, you have a file object. you can obtain various information about the file.The following lists all attributes related to the file object:Attribute description
File. closedReturns true if the object is closed; otherwise, returns false.
File. modeReturns the access mode of the opened file.
File. nameThe name of the returned file.
File. softspaceIf the print output must be followed by a space character,
string in the form of a string.
>>> F = open ('test', 'w ')
# Use the open function to define the file name and processing mode. the file will be created in the current linux directory.
Supported modes:
'R' is opened as read by default.
'W' first clears the file and writes it when it is opened.
'X' creates a new file and opens it for writing.
'A' open the write and append it to the end of an existing file.
>>> Dir (f) # view the methods supported by this object
['_ Class _', '_ delattr _', '_ d
file. However, although there are multiple line breaks, reading Python should be replaced by \ n. + B t can be added after the pattern character, indicating that the file can be read and written at the same time and the file can be opened in binary mode and text mode (default.If buffering is set to 0, no buffer is performed. If it is set to 1, "Row buffer" is performed. If it is a number greater than 1, the buffer size is measured in bytes.
File objects have their own attributes and methods. Le
new file to read and write.
Properties of the File objectAfter a file is opened, you have a files object that you can get various information about the file.The following is a list of all properties related to the file object:
Properties
Description
File.closed
Returns true if the file has been closed, otherwise false is returned.
File.mode
Returns the access mode of the file being opened.
File.name
Return
methods. Let's take a look at the properties of the file.
Closed #标记文件是否已经关闭, rewritten by close ()
Encoding #文件编码
Mode #打开模式
Name #文件名
Newlines #文件中用到的换行模式, is a tuple
Softspace #boolean型, typically 0, is said to be used for print
File read and Write method:
F.read ([size]) #size为读取的长度, in bytes
F.readline ([size])
#读一行, if size is defined, it is possible to return only a portion of a row
F.readlines ([size])
#把文件每一行作为一个list的一个成员, and re
', ' readlines ', ' seek ', ' softspace ', ' Tell ', ' Truncate ', ' W Rite ', ' writelines ', ' xreadlines ']
And then some of the properties of the detailed description, that is, reader learning.
Open File
A file was created under a folder, named 130.txt, and entered in the following:
learn python
http://qiwsir.github.io
qiwsir@gmail.com
This file is for three lines.
The following figure shows where this file is stored:
In the screensh
is said that '/R '/n '/'/r/n ' can represent line break, A tuple is used to store the newline characters used in this file. However, although there are multiple modes for line breaks, read the unified use of/n instead in Python. After the pattern character, you can also add a + B t these two identities, respectively, can be read and write to the file and in binary mode, text mode (the default) to open the file.Buffering if 0 means no buffering, if 1 means "row buffer", or if a number greater th
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.