Python ยท files

File Paths

Check whether files exist, build paths that work on every system, and take paths apart with os.path and pathlib.

os module: basic checks


        import os
        # check if exists
        os.path.exists("file.txt")
        # ans: True or False
        # check if file
        os.path.isfile("file.txt")
        # ans: True or False
        # check if directory
        os.path.isdir("folder")
        # ans: True or False
      

os module: path info


        import os
        # file size
        os.path.getsize("file.txt")
        # ans: size in bytes
        # absolute path
        os.path.abspath("file.txt")
        # ans: /full/path/to/file.txt
      

abspath glues the current working directory onto a relative path.

os module: joining paths


        import os
        # join paths
        os.path.join("folder", "file.txt")
        # ans: folder/file.txt
        # ans: works across platforms
      

os module: splitting paths


        import os
        # directory name
        os.path.dirname("/path/to/file.txt")
        # ans: "/path/to"
        # base name
        os.path.basename("/path/to/file.txt")
        # ans: "file.txt"
        # split extension
        os.path.splitext("file.txt")
        # ans: ("file", ".txt")
      

The pathlib module


        from pathlib import Path
        # create path object
        path = Path("file.txt")
        # check exists
        path.exists()
        # ans: True or False
        # read file
        content = path.read_text()
      

A Path is an object with methods, instead of a plain string passed to os.path functions.

pathlib: path parts


        from pathlib import Path
        path = Path("/folder/file.txt")
        # name
        path.name
        # ans: "file.txt"
        # stem
        path.stem
        # ans: "file"
        # suffix
        path.suffix
        # ans: ".txt"
        # parent
        path.parent
        # ans: Path("/folder")
      

The anatomy of a path

pathlib names on top, the matching os.path function underneath. os.path.splitext splits off the same suffix, which is only the last extension.

Exercises

Predict, then reveal.

Exercise 1


        import os
        # exists vs isfile?
        os.path.exists("folder")
        # ans: True
        os.path.isfile("folder")
        # ans: False
      

Exercise 2


        import os
        # join paths?
        os.path.join("a", "b", "c")
        # ans: "a/b/c" (or "a\\b\\c" on Windows)
      

Exercise 3


        import os
        # splitext?
        os.path.splitext("file.tar.gz")
        # ans: ("file.tar", ".gz")
      

Exercise 4


        from pathlib import Path
        # path / operator?
        Path("folder") / "file.txt"
        # ans: Path("folder/file.txt")
      

Exercise 5


        from pathlib import Path
        # suffix with no extension?
        Path("file").suffix
        # ans: ""
      

Exercise 6


        import os
        # abspath on absolute?
        os.path.abspath("/a/b/c")
        # ans: "/a/b/c" (unchanged)
      

Exercise 7


        from pathlib import Path
        # multiple suffixes?
        Path("file.tar.gz").suffixes
        # ans: [".tar", ".gz"]
      

Exercise 8


        import os
        # basename of directory?
        os.path.basename("/path/to/")
        # ans: ""
        os.path.basename("/path/to")
        # ans: "to"
      

basename is just the text after the last slash, so a trailing slash leaves nothing.

Exercise 9


        from pathlib import Path
        # parent of parent?
        Path("/a/b/c").parent.parent
        # ans: Path("/a")
      

Exercise 10


        import os
        # current directory?
        os.getcwd()
        # ans: returns current working directory
      

Remember

Check

exists is True for files and folders; isfile and isdir tell them apart.

Build

os.path.join or Path(...) / "name" pick the right separator for the system.

Take apart

parent/dirname, name/basename, stem and suffix/splitext.

Where am I?

Relative paths start from os.getcwd(); abspath makes them absolute.