Usecase

The aim is describe python’s approach to opening/creating files

1. On Resources

  • resources are elements of a program that must be released/closed after use
  • resource management is critical for proper program functioning
  • files are a type of resources
  • python provides special syntax and protocol to work with them

2. Open a file with open()

  • open() is a built-in function to open a file
  • there are three parameters
PARAM DESC
file required; filename
mode optional; explicit is better than implicit (read, write, append, binary, text)
encoding optional;

3. Mode

  • mode determines the exact type of object being opened
  • this is dynamic typing in action
MODE MEANING
r open for reading
w open for writing
a open for appending
SELECTOR MEANING
t text-mode
b binary-mode
  • e.g. wb for write-binary, or at for append-text
  • be explicit for the sake of readibility

4. Encoding

  • getting the encoding right is crucial for properly encoding the content of the text file
  • if not specified, python will use the default encoding from
 import sys
 sys.getdefaultencoding()
 'utf-8'
  • that however, is not guaranteed and encoding should be checked properly

5. File-like object

  • as said, mode determines the exact type
  • in general, you can expect the object to support certain methods and contain certain properties

6. Test path idiom

In powershell, there is an simple idiom

if (test-path $file) {
    Invoke-Item $file
} else {New-Item $file}

… which checks if $file is there and then either opens it or creates it How to go about this in Python ?

  • you also use open() but in a different mode, in a writing mode
  • if you invert the logic (if it does not exist, create the file)
>>> import os
>>> docPath = ".\test.md"   
>>> os.path.exists(docPath)
False
# create and close
>>> f = open("test.md",'w') 
>>> f.close()
# test               
>>> docPath = "./test.md"   
>>> os.path.exists(docPath) 
True
>>> f.name
'test.md'
>>>
  • note that the file object does not contain the path property

7. Path compatibility

  • also it seems that not only you have universal newline
  • but if you are within the python REPL/interpreter and you are not passing paths from windows shell, you can securely run with unix paths with forward slashes