![]() We import a module when we want to use its code in our script. Importing a module means gaining access to all the functions and variables that are stored within the module. To use the os module in your script, you need to "import" it. A module is a file that contains related Python code.An example of a directory is your "Documents" folder. A directory is what we commonly know as a "folder", where we usually store related files and/or other directories, creating a hierarchy of directories within directories that are called subdirectories. ![]() Displaying the path to your current working directory.This module includes functions that are used to interact with your operating system, performing actions like: The two functions that we will discuss: listdir() and system() belong to the os module. Examples of both of them and how they work behind the scenes.How to work with the system("ls") function and when to use it.How to work with the listdir function and when to use it. ![]() Then, we will dive into the functions themselves: ![]() The difference between an absolute path and a relative path.How to check and change your current working directory.The concepts of "directory" and "current working directory".The built-in Python os module and how to import it.We will start by diving into concepts that are essential to work with listdir and system: If you want to learn how these functions work behind the scenes and how you can use their full power, then this article is for you. ![]() old_wd /opt/R/4.3.2/lib/R/share/R #> /opt/R/4.3.2/lib/R/share/Rd #> /opt/R/4.3.2/lib/R/share/dictionaries #> /opt/R/4.3.2/lib/R/share/encodings #> /opt/R/4.3.2/lib/R/share/java #> /opt/R/4.3.2/lib/R/share/licenses #> /opt/R/4.3.2/lib/R/share/make #> /opt/R/4.3.2/lib/R/share/sh #> /opt/R/4.3.2/lib/R/share/texmf # Create a shorter link link_create ( system.file (package = "base" ), "base" ) dir_ls ( "base", recurse = TRUE, glob = "*.R" ) #> base/demo/ base/demo/is.things.R #> base/demo/recursion.R base/demo/scoping.R # If you need the full paths input an absolute path dir_ls ( path_abs ( "base" ) ) #> /tmp/RtmpHFCmia/base/CITATION /tmp/RtmpHFCmia/base/DESCRIPTION #> /tmp/RtmpHFCmia/base/INDEX /tmp/RtmpHFCmia/base/Meta #> /tmp/RtmpHFCmia/base/R /tmp/RtmpHFCmia/base/demo #> /tmp/RtmpHFCmia/base/help /tmp/RtmpHFCmia/base/html dir_map ( "base", identity ) #> ] #> "base/CITATION" #> #> ] #> "base/DESCRIPTION" #> #> ] #> "base/INDEX" #> #> ] #> "base/Meta" #> #> ] #> "base/R" #> #> ] #> "base/demo" #> #> ] #> "base/help" #> #> ] #> "base/html" #> dir_walk ( "base", str ) #> chr "base/CITATION" #> chr "base/DESCRIPTION" #> chr "base/INDEX" #> chr "base/Meta" #> chr "base/R" #> chr "base/demo" #> chr "base/help" #> chr "base/html" dir_info ( "base" ) #> # A tibble: 8 × 18 #> path type size permissions modification_time user group #> #> 1 base/CITATION file 643 rw-r-r- 22:39:44 root root #> 2 …se/DESCRIPTION file 354 rw-r-r- 22:39:44 root root #> 3 base/INDEX file 24.1K rw-r-r- 22:39:44 root root #> 4 base/Meta dire… 4K rwxr-xr-x 01:14:31 root root #> 5 base/R dire… 4K rwxr-xr-x 01:14:31 root root #> 6 base/demo dire… 4K rwxr-xr-x 01:14:31 root root #> 7 base/help dire… 4K rwxr-xr-x 01:14:31 root root #> 8 base/html dire… 4K rwxr-xr-x 01:14:31 root root #> # ℹ 11 more variables: device_id, hard_links, #> # special_device_id, inode, block_size, blocks, #> # flags, generation, access_time, #> # change_time, birth_time # Cleanup link_delete ( "base" ) setwd (. ![]()
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |