• Skip to secondary menu
  • Skip to main content
  • Skip to primary sidebar

Python and R Tips

Learn Data Science with Python and R

  • Home
  • Python
  • Pandas
    • Pandas 101
  • tidyverse
    • tidyverse 101
  • R
  • Linux
  • Conferences
  • Python Books
  • About
    • Privacy Policy
You are here: Home / Python / 7 Tips to Play With File Names, Directory Names, and Paths in Python

7 Tips to Play With File Names, Directory Names, and Paths in Python

July 28, 2012 by cmdlinetips

Python’s OS.path module have a lots of commands/methods that are greatly useful for all things files/directories/paths. Here are seven tips for playing with files, directories, and paths using Python os.path. These commands will help you check if a given path is a directory or not? find given path is a file? check if a file exists, get size of a file, get base name of the path and get extension of a file.

How To Check If a “Name” is a Directory?

To make sure if a name is directory or not, one can use “os.path.dir” to check it. “os.path.dir(name)” will return “True”, if it is a directory and “False” if it is not.

> os.path.isdir(dir_name)
True
> os.path.isdir(file_name)
False

How To Check If a “Name” is a File?

Similarly, one can use “os.path.isfile(name)” to check if the name is a file or not. Note that “os.path.isfile” will return false, if there is no file with the name.

> os.path.isfile(file_name)
True
> os.path.isfile(dir_name)
False
# name of a file that does not exist
> os.path.isfile(file_name)
False

How To Check If a File Exists?

To check if a file exists in a directory, use “os.path.exists”.

> os.path.exists(file_name)
False
> os.path.exists(file_name)
True

How To Get File Size?

To get the size of a file, use “os.path.getsize”. It will give file size in bytes.

>os.path.getsize(file_name)
63063

How To Split Path and Get File/Directory Name?

There are numerous ways to split a path and get the directory/file name at the path and get the base name from the path.

The Python command “os.path.split(path2File)” splits the path in to head and tail, where tail is the last part in the path. Python documents says that

The tail part will never contain a slash; if path ends in a slash, tail will be empty. If there is no slash in path, head will be empty. If path is empty, both head and tail are empty. Trailing slashes are stripped from head unless it is the root (one or more slashes only).

For example

> path2File = "/Users/cmdlinetips/pythonTips.pdf"
> os.path.split(path2File)
('/Users/cmdlinetips', 'pythonTips.pdf')

Another way to get the file name (or directory name) from a path is to use “os.base.basename”. Here is an example

> os.path.basename(path2File)
'pythonTips.pdf'

How To Get the Extension of a File Name from a Path?

If you just want to get the extension of a file from a path, use “os.path.splitext” and the second element from the tuple is the extension of file name.

> os.path.splitext(path2File)
('/Users/cmdlinetips/pythonTips', '.pdf')

If your path is a directory, “os.path.splitext” will have an empty string at the second element at the output tuple.

path2File = '/Users/cmdlinetips/pythonTips'
> os.path.splitext(path2File)
('/Users/cmdlinetips/pythonTips', '')

 

>os.path.dirname(path2File)
'/Users/cmdlinetips'

How to Get The Complete Path to a File in Current Directory?

The Python command “os.path.abspath” gives the full/complete path to a file in current directory.

> os.path.abspath('pythonTips.pdf')
/Users/cmdlinetips/pythonTips.pdf
> os.path.abspath("")
/Users/cmdlinetips

Share this:

  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on X (Opens in new window) X

Related posts:

Default ThumbnailHow To Check If a File or Directory Exists Using Shell Script? Default Thumbnail3 Ways to Read A Text File Line by Line in Python Default ThumbnailHow to Download a File or Directory Using wget? Default ThumbnailGetting Started With Python Dictionaries: Python Tips

Filed Under: Python, Python os.path tips Tagged With: Python, Python OS Module, Python os.path, Python Tips

Primary Sidebar

Subscribe to Python and R Tips and Learn Data Science

Learn Pandas in Python and Tidyverse in R

Tags

Altair Basic NumPy Book Review Data Science Data Science Books Data Science Resources Data Science Roundup Data Visualization Dimensionality Reduction Dropbox Dropbox Free Space Dropbox Tips Emacs Emacs Tips ggplot2 Linux Commands Linux Tips Mac Os X Tips Maximum Likelihood Estimation in R MLE in R NumPy Pandas Pandas 101 Pandas Dataframe Pandas Data Frame pandas groupby() Pandas select columns Pandas select_dtypes Python Python 3 Python Boxplot Python Tips R rstats R Tips Seaborn Seaborn Boxplot Seaborn Catplot Shell Scripting Sparse Matrix in Python tidy evaluation tidyverse tidyverse 101 Vim Vim Tips

RSS RSS

  • How to convert row names to a column in Pandas
  • How to resize an image with PyTorch
  • Fashion-MNIST data from PyTorch
  • Pandas case_when() with multiple examples
  • An Introduction to Statistical Learning: with Applications in Python Is Here
  • 10 Tips to customize ggplot2 title text
  • 8 Plot types with Matplotlib in Python
  • PCA on S&P 500 Stock Return Data
  • Linear Regression with Matrix Decomposition Methods
  • Numpy’s random choice() function

Copyright © 2025 · Lifestyle Pro on Genesis Framework · WordPress · Log in

Go to mobile version