Opening and Creating Files with Python

Pavol Kutaj
2 min readJan 21, 2021

--

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

PARAMDESCfilerequired; filenamemodeoptional; explicit is better than implicit (read, write, append, binary, text)encodingoptional;

3. Mode

  • mode determines the exact type of object being opened
  • this is dynamic typing in action

MODEMEANINGropen for readingwopen for writingaopen for appendingSELECTORMEANINGttext-modebbinary-mode

  • e.g. wb for write-binary, or at for append-text
  • be explicit for the sake of readability

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, the 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 a 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 a universal newline
  • but if you are within the python REPL/interpreter and you are not passing paths from the windows shell, you can securely run with unix paths with forward slashes

--

--

No responses yet