Don't red-headed with the old Ziko Python (2)

Source: Internet
Author: User
properties of the file

The so-called attribute is something that can be obtained through a file object.

Copy the Code code as follows:


>>> f = open ("131.txt", "a")
>>> F.name
' 131.txt '
>>> F.mode #显示当前文件打开的模式
A
>>> f.closed #文件是否关闭, returns True if off, returns false if open
False
>>> f.close () #关闭文件的内置函数
>>> f.closed
True

Status of the file

Many times, we need to get a file about the state (sometimes become a property, but here the file attributes and the above file attributes are not the same, but I think that the file status is better), such as the creation date, access date, modification date, size, and so on. In the OS module, there is a way to resolve this issue:

Copy the Code code as follows:


>>> Import OS
>>> File_stat = Os.stat ("131.txt") #查看这个文件的状态
>>> File_stat #文件状态是这样的. From the content below, there are a lot of words from the English can be guessed out.
Posix.stat_result (st_mode=33204, st_ino=5772566l, st_dev=2049l, St_nlink=1, st_uid=1000, st_gid=1000, St_size=69L, St _atime=1407897031, st_mtime=1407734600, st_ctime=1407734600)

>>> File_stat.st_ctime #这个是文件创建时间
1407734600.0882277 #换一种方式查看这个时间
>>> Import Time
>>> time.localtime (file_stat.st_ctime) #这回看清楚了.
Time.struct_time (tm_year=2014, tm_mon=8, tm_mday=11, tm_hour=13, tm_min=23, tm_sec=20, tm_wday=0, tm_yday=223, tm_ isdst=0)

The above information about file status and file attributes may be used when judging and manipulating certain aspects of the file. Specifically, file attributes. For example, in the operation of the file, we often have to determine whether the file is closed or open, you need to use the file.closed attribute to judge.

Built-in functions for files

Copy the Code code as follows:


>>> dir (file)
[' __class__ ', ' __delattr__ ', ' __doc__ ', ' __enter__ ', ' __exit__ ', ' __format__ ', ' __getattribute__ ', ' __hash__ ', ' __init ' __ ', ' __iter__ ', ' __new__ ', ' __reduce__ ', ' __reduce_ex__ ', ' __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 ', ' xreadline S ']
>>>

So many built-in functions, not all told, can only pick up the focus of the experiment.

Copy the Code code as follows:


>>> f = open ("131.txt", "R")
>>> F.read ()
' My name is Qiwsir.\nmy website are qiwsir.github.io\naha,i like program\n '
>>>

File.read () is able to read all the contents of the file. In particular, this is to return a string and to read all the contents of the file into memory. Imagine, if too much content is not a bit miserable? Really, don't read the big file.

Copy the Code code as follows:


>>> contant = F.read ()
>>> type (contant)

If the file is larger, do not read it all at once, you can turn to one line and use ReadLine

Copy the Code code as follows:


>>> f = open ("131.txt", "R")
>>> F.readline () #每次返回一行 and the pointer moves down
' My name is qiwsir.\n '
>>> f.readline () #再读, and then return a row
' My website is qiwsir.github.io\n '
>>> F.readline ()
' Aha,i like program\n '
>>> f.readline () #已经到最后一行了, re-read, no error, return empty
''

This method, crossing not feel too slow? Do you have a good point? There, please brandished from the palace, not self-palace, can also use ReadLines. Note the difference, this is the plural, the implication is more line.

Copy the Code code as follows:


>>> f = open ("131.txt", "R")
>>> cont = F.readlines ()
>>> cont
[' My name is qiwsir.\n ', ' My website are qiwsir.github.io\n ', ' aha,i like program\n ']
>>> Type (cont)

>>> for line in cont:
.. Print Line
...
My name is Qiwsir.

My website is Qiwsir.github.io

Aha,i like program

From the experiment we can see that readlines and read have the same place, is to read the contents of the file once, stored in memory, but there is a difference, read returns the STR type, ReadLines returns a list, and a row of an element, so, You can print it out by the for line.

In print line, look at each element in the list and end with a \ n ending, so the printed result will be blank. The reason has been introduced before, forget the friend roll back to the previous talk

However, it is still necessary to remind yours faithfully that too large files are not read into memory. It is recommended to deal with larger files:

Copy the Code code as follows:


>>> f = open ("131.txt", "R")
>>> F

>>> type (f)

>>> for line in F:
... print line
...
My name is Qiwsir.

My website is qiwsir.github.io

Aha,i like program

The above are all built-in functions and methods for reading files. In addition to reading, it is to write. The so-called write is to deposit the content into a file. The built-in function used is write. However, to write to the file, also pay attention to open the file mode, either W or a, depending on the situation.

Copy the code code as follows:


>>> f = open ("131.txt", "a") #因为这个文件已经存在, I do not want to empty, with Append mode
> >> F.write ("There is a baby.") #这句话应该放到文件最后
>>> f.close () #请看官注意, after writing, be sure to close the file in a timely manner. To represent the actual write to the

To see how the Write works:

Copy code code as follows:


>>> f = open ("131.tx T "," R ")
>>> for line in F.readlines ():
... print line
...
My name is Qiwsir.

My website is qiwsir.github.io

Aha,i like program

There is a baby. #果然增加了这一行


above is off To the basic operation of the file. In fact, the document is far from the knowledge of these, interested in crossing can Google pickle This module, is a very useful thing.
  • 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.