程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python Advanced Series (XV)

編輯:Python

open function

open Function to open a file . It's super simple ? Most of the time , We see it being used like this :

f = open('photo.jpg', 'r+')
jpgdata = f.read()
f.close()

The reason why I am writing this article now , Most of the time I see open Used in this way . There are three errors in the above code . Can you point them all out ? If not , Please read on . At the end of this article , You will know where the above code is wrong , and , what's more , You can avoid these errors in your own code . Now let's start with the basics :

open The return value of is a file handle , Entrusted to you from the operating system Python Program . Once you're done with the file , You will want to return the file handle , Only in this way can your program not exceed the maximum number of file handles that can be opened at one time .

Call... Explicitly close Closed the file handle , But only if read In case of success . If there is any exception, it happens to be f = open(...) After that ,f.close() Will not be called ( Depending on Python The practice of the interpreter , The file handle may still be returned , But that's another topic ). To ensure that no matter whether the exception is triggered or not , Files can be closed , We wrap it into a with sentence :

with open('photo.jpg', 'r+') as f:
    jpgdata = f.read()

open The first parameter of is the filename . the second (mode Turn on mode ) Determines how the file is opened .

  1. If you want to read a file , Pass in r
  2. If you want to read and write files , Pass in r+
  3. If you want to overwrite the write file , Pass in w
  4. If you want to add something to the end of the file , Pass in a

Although there are several other effective mode character string , But it is possible that you will never use them .mode Very important , Not just because it changes behavior , And it may cause permission errors . for instance , If we open a in a write protected Directory jpg file , open(.., 'r+') And failed .mode May contain an extended character ; Let's also open the file in binary mode ( You will get a string of bytes ) Or text mode ( character string )

Generally speaking , If the file format is written by someone , Then it is more likely to be text mode .jpg Image files are generally not written by people ( And it is not directly readable by people ), So you should open them in binary mode , The method is in mode String followed by a b( You can look at the example at the beginning , The right way is to rb).

If you open something in text mode ( such as , Add one more t, Or just use r/r+/w/a), You must also know which code to use . For computers , All files are bytes , Not characters .

unfortunately , stay Pyhon 2.x In the version ,open Specifying the encoding explicitly is not supported . However ,io.open Function in Python 2.x neutralization 3.x( Where it is open Another name for ) All of them provide , It can do the right thing . You can pass in encoding This keyword parameter is used to pass in the encoding .

If you don't pass in any code , A system - as well as Python - The specified default option will be selected . You may be tempted to rely on this default option , But this default option is often wrong , Or the default encoding cannot actually express all the characters in the file ( This will often happen in Python 2.x and / or Windows).

So pick a code .utf-8 Is a very good code . When you write a file , You can choose a code you like ( Or the code that the program that eventually reads your file likes ).

How do you find out what kind of code the file you are reading is written in ? ok , Unfortunately , There is no very simple way to detect code . In different codes , The same bytes can represent different , But equally valid characters . therefore , You have to rely on a metadata ( such as , stay HTTP In the head message ) To find the code . More and more , The file format defines the encoding as UTF-8.

With these basics , Let's write a program , Read a file , Check if it is JPG( Tips : These file headers are in bytes FF D8 Start ), Write the description of the input file into a text file .


import io
with open('photo.jpg', 'rb') as inf:
    jpgdata = inf.read()
if jpgdata.startswith(b'\xff\xd8'):
    text = u'This is a JPEG file (%d bytes long)\n'
else:
    text = u'This is a random file (%d bytes long)\n'
with io.open('summary.txt', 'w', encoding='utf-8') as outf:
    outf.write(text % len(jpgdata))

I'm sure , Now you can use it correctly open La !


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved