Directory Navigation
I LOVE DOING THIS :)
Directory details ?
Methods for traversing directories
Listing files and thier information
Creating and Deleting directories + files
Test to check if something is a file or directory
Inorder to perform all this, we'll have to import the os module
>>> import os
>>> os.getcwd() // Prints Current Working Directory
/root/
>>>
>>> os.listdir("/") // Lists the root directory within a list
Let's quickly write a program to check if a file is actually a file or a directory
#!/opt/python3
for i in os.listdir("."):
if os.path.isfile(i):
print(i + "is a file")
print("-----------------")
elif os.path.isdir(i):
print(i + "is a directory")
print("-----------------")
os.path.isfile()
andos.path.isdir()
checks for the file or the directory
So, now how do we search for specific kind of files ? How do we grep them ?
>>> for i in glob.glob(os.path.join(".", "*.py")):
... print(i)
...
~./file_desc.py
~./navigation.py
~./source.py
Exercise Question
Create a program which takes the path of directory as input and from that directory it checks all the files within it and makes sure if it's really a directory or a file, and prints them in a hierarchical way ?
For any given filename list out all the stats related to the file such as size, creation time, path
#!/opt/python3
import os
import sys
import pwd #Password db module
file = os.stat(sys.argv[1])
if file:
print("File name is: %s" % sys.argv[1])
print("The exact path: " + os.path.join(os.path.abspath(sys.argv[1])))
print("File size: %d" % file.st_size + " bytes")
print("File Owners UID is: %d" % file.st_uid)
print("File Owners name: %s" % pwd.getpwuid(file.st_uid).pw_name)
os.stat( path )
- Get the status of a file or a file descriptor. Perform the equivalent of a stat()
system call on the given path.
st_size - Size of the file in bytes, if it is a regular file or a symbolic link
st_uid - User identifier of the file owner
import pwd
- This module provides access to the Unix user account and password database
pwd.getpwuid(uid) - Return the password database entry for the given numeric user ID
pw_name - Login name attribute in the pwd module
pw_passwd - Displays the password in an encrypted format
os.path.join("path", "to", "folder")
- It can handle join multiple paths, and take care of the right syntax for the current operating system
os.path.abspath() - This function will convert the relative path to an absolute path by prefixing the current working directory to the path, Basically returns the absolute path of the
sys.argv[1]
Another noob way to perform the above task would be directly calling the methods and attributes
#!/opt/python3
import os
import sys
statinfo = os.path.join(sys.argv[1])
ctime = os.path.getctime(statinfo)
path = os.path.abspath(statinfo)
size = os.path.getsize(statinfo)
metadata = os.stat(statinfo)
print("The file creation time: " + str(ctime))
print("The exact path: " + path)
print("The size is: " + str(size) + " bytes")
print("Finally the metadata: " + str(metadata))
Last updated