Keep in mind that the mkdir() method cannot create sub-directories on a deeper level than one in a single call. Print ( "Successfully created the directory %s " % path) Print ( "Creation of the directory %s failed" % path) # define the name of the directory to be created For an example, see the code below: import os As a parameter, mkdir() first requires the path name for the directory in order for it to be created. Creating a DirectoryĬreating a single directory is done using the mkdir() method. But for now we'll move on to the main purpose of this article. There are quite a few other directory operations not covered in this article, like checking if a file or directory exists. This one is similar to the getcwd() method but returns the path as a binary string, instead. The current working directory is /home/frank/įurthermore, the os module contains the additional getcwdb() method. The output should look something like this: $ python3 cwd.py Print ( "The current working directory is %s" % path) # detect the current working directory and print it Listing 1 shows how to integrate this method in a Python script. This method will return a string containing the path of your working directory. Detecting the Current Working Directoryīefore we get in to creating/removing directories, let's see how to perform some other basic directory operations, like detecting the current working directory using the method getcwd(). However, as you'll see later on, if you want to something more advanced, like create a temporary file for storing data, then we'll also be needing the tempfile module. The os module contains most of the methods we'll need throughout this article. It is done using an import statement as follows: import os os is a Python module which belongs to the core part of the Python ecosystem. Reading and writing files does not require loading an additional module, but accessing the file system functions (like handling directories) requires that we use a separate module. Therefore, the two Python modules os and tempfile come into play. Next, we will show you how to identify the current working directory, how to create both a persistent and a temporary, single directory as well as nested directory structures with subfolders, and how to remove a directory if no longer needed. Other operating systems, like UNIX/Linux, instead use a different terminology, where an "entry" is named a "folder". This article focuses on the handling of directories. Reading and writing from and to them is done in a similar way as we saw in the previous articles. There are also various other types of entries such as files, directories, sockets (for inter-process communication), named pipes, both soft and hard links, as well as special files (block devices). Interestingly, the file system is much more than a way to store/retrieve data to disk. The previous articles dealt with reading and writing files. This article continues with our series on interacting with the file system in Python.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |