Os.Path.Join

Os.Path.Join Blanc Media LTD

Python is growing in popularity due to its relative ease of learning, versatility, and other benefits. True, novice developers often have problems working with files and the file system. Simply because they don`t know all the commands you need to know.
2021-11-21, by ,

#Os Path || #Python || #Methods ||

Table of contents:



This module can be used in many cases. We will not cover each of them in this section. Instead, we'll take a look at exactly how it can be used and also take a look at a submodule called os.path. In particular, we will talk about:

  • os.name
  • os.environ
  • os.chdir ()
  • os.getcwd ()
  • os.getenv ()
  • os.putenv ()
  • os.mkdir ()
  • os.makedirs ()
  • os.remove ()
  • os.rename ()
  • os.rmdir ()
  • os.startfile ()
  • os.walk ()
  • os.path

os.environ, os.getenv () and os.putenv ()

The os.environ value is known as a mapping object that operates on a dictionary of user-environment variables. You might not know, but every time you use your computer, some environment variables are already set.

os.path

The os.path submodule of the os module has many built-in benefits. Let's take a look at the following features (the main source was https://python.org/python-os-path-join-method/)

  • os.path.basename
  • os.path.dirname
  • os.path.exists
  • os.path.isdir and os.path.isfile
  • os.path.join
  • os.path.split

These are not all the functions of the submodule. We can get acquainted with them later.

Combining path components

In the previous example, I deliberately used a slash "/" to separate the path components. This is fine in principle, but not recommended. If you want your application to be cross-platform, this option is not suitable. For example, some older versions of Windows only recognize the slash "" as a separator.

But don't worry, Python solves this problem perfectly thanks to the os.path.join () function. Let's rewrite the example from the previous paragraph using this function:

os.path.exists (os.path.join ('sample_data', 'README.md'))

Another tip for creating directories. Sometimes we need to create subdirectories with a nesting level of 2 or more. If we are still using os.mkdir (), we will need to do this several times. In this case, we can use os.makedirs (). This function will create all intermediate directories just like the mkdir -p flag on a Linux system:

Show directory contents

Another useful command is os.listdir (). It shows the entire contents of the directory.

The command differs from os.walk (), where the latter recursively shows everything "under" the directory. os.listdir () is much easier to use because it just returns a list of the contents:

os.listdir ('sample_data')

Moving files

It's time to try moving files from one folder to another. The recommended way is another built-in shutil library.
Now let's try to move all CSV files from the "sample_data" directory to the "test_dir" directory.

Boss Heights

Boss Heights contributor to blancmedia.co.uk
Public figure