Saturday, September 30, 2023

python module commands

#### To get module help information details :- help(module)

example : - help(os) 

 os.get_exec_path()

os.getenv('ORACLE_HOME')

os.environ.get('ORACLE_HOME')

To set environ 

os.environ['ORACLE_HOME'] = '/u02/app/oracle/product/19c'

os.uname()

os.path.getmtime(path) ---> get modified time

import os, sys, time 

local_time = time.ctime(os.path.getmtime(path))  # get time in local format 

os.path.size(path/(1024*1024*1024)) # get size in GB default bytes 

os.path.isabs(path) # check if path is absolute boolean

os.path.isfile(path) # check if path is file boolean

os.path.isdir(path) # check if path is directory boolean

os.path.islink(path) # check if file is link boolean

os.path.ismount(path) # check if the block device is mounted or not boolean

os.path.samefile(path1,path2) # check if the files are same boolean

os.symlink(source,destination) # create soft link

os.link(source,destination) # create hard link

os.access(path,os.F_OK) #check files exists boolean

os.access(path,os.R_OK) # check file is readable boolean

os.access(path,os.W_OK) # check file is writable boolean

os.access(path,os.X_OK) # check file is executable boolean

print(os.path.abspath(__file__)) # show the self script name 


SYS 

import sys

command_line_user_input = sys.stdin.readline()  # it will wait for your terminal input will record it

print(command_line_user_input)

# writing to stdout

sys.stdout.write("This is stdout. \n")

# writing to stderr

sys.stderr.write("This is stderr. \n")

# sys exit

sys.exit(1)

# linux/windows info 

print(sys.platform)

# linux version 

print(sys.version)

# modules 

for module in sys.modules:

    print(module)

# to execute os commands 

os.system("command")

# to create directory

os.mkdir(path)

# to get current working directory

os.getcwd()

# to change directory

os.chdir(path)

# to change join path

os.path.join(path,new_file)

# to truncate file size in bytes 

os.truncate(path,length) # length = 1024*1024*1024 ~ 1GB

# to get basename 

os.path.basename(path)  # will return only file/dir name 

# to get relative path

os.path.relpath(path)

# to get absolute path

os.path.abspath(path)

# check if file dir exists or not boolean

os.path.exists(path) 

# to create directory 

os.mkdir(path)

# to delete directory

os.rmdir(path)

# to delete file 

os.remove(path)

# Using a with statement to open and automatically close the file 👉

Opening a File:-
To open a file, you can use the open() function with the desired file name and mode. The common modes are:
'r': Read (default mode).
'w': Write (creates a new file or truncates an existing file).
'a': Append (opens a file for writing in append mode, creating a new file if it doesn't exist).
'x': Exclusive creation (creates a new file but raises an error if it already exists).
'b': Binary mode (e.g., 'rb' for reading a binary file).

with open('example.txt', 'w') as file:
...     file.write("hello")
...     file.write("this is second line.\n")
...     file.write("this is third line. \n")

# without using with open :-
>>> file = open('example.txt', 'w')
>>> file.write("Hello, world! \n")
15
>>> file.write("second line.\n")
13
>>> file.write("third line.\n")
12
>>> file.close()

# to read lines from above file  👉
with open('example.txt', 'r') as file:
...    lines = file.readlines()
...    for line in lines :
...          print(line.strip())

# to read lines using 👉
with open('example.txt', 'r') as file :
...     lines = [line.strip() for line in file]
...     for line in lines:
...             print(line)

# to read line simply 👉 
with open('example.txt', 'r') as file:
...     for line in file:
...             print(line.strip())

No comments:

Post a Comment

virtual environment on python on Ubuntu

 # 1. Update system and install Python tools sudo apt update && sudo apt install python3-pip python3-venv -y # 2. Setup Django in a ...