Sunday, May 24, 2026

Python Material - Part - 29 - sys_shutil_module

 __author__ = "Narendra Boyina"


# from Class_33_OS_2_module.os_2 import json_file_name

# -----------------------------------------------------------------------------
# Copyright (c) 2025 BR Technologies PVT LTD
# -----------------------------------------------------------------------------
"""
Topics to be covered in today's class:
--> sys module
--> shutil module
"""
##################### sys module (system module) #################################
#
# print("Suresh & Indhu") # constant value
# x = 4 # constant value
# x = int(input("enter any value")) # (while execution) whenever required user provides input data
"""To input data, the operator/Executor must be ready in front of the laptop when prompted by the program,
which can be time-consuming and inefficient."""

# import string
# print(dir(string))


import sys
# print(help(sys))
"""
system module mainly used for below purpose
1. sys module is mainly responsible for the interaction between a program and the interpreter
2. To get a list of all paths where Python was installed on your machine (like Python DLL files and site-packages location,)
3 command line arguments

Note: open command prompt ==> change directory to PWD/CWD (current working directory/where your python script is located)
==> type as==> python file_name.py 12 12 64 45"""

print("Raahi")
# print(sys.argv) # by default it will give where python file saved
source_file_nmae = sys.argv[0]
print(source_file_nmae)

x_value = sys.argv[1] # it will take filename as 0th argument
print(x_value)

json_file_name = sys.argv[2]
print(json_file_name)

txt_file_name = sys.argv[3]

# x = sys.argv[0:4] # it will ignore file name (i.e it will take only values from 1 to 3 )
# x = sys.argv[1:5] # it will ignore file name & 1st value (i.e it will take only values from 2 to 4 )
# print(x)
# name = input("Enter your name:")
# age = input("Enter your age: ")
# print("My name is {-1}. and i'm {1} years old!.".format(name, age))


# x = sys.argv[0:]
# print(x)

from sys import argv

# x = argv[0:]
# print(x)
# x = argv[0]
# print(x)
# x = argv[0:4]
# print(x)
# y = argv[1]
# print(y)
# z = argv[2]
# print(z)
# i = argv[3]
# print(i)

import sys
# print(sys.builtin_module_names)
"""
output:
('_abc', '_ast', '_bisect', '_blake1', '_codecs', '_codecs_cn',
'_codecs_hk', '_codecs_iso2021', '_codecs_jp', '_codecs_kr', '_codecs_tw', '_collections', '_csv',
'_datetime', '_functools', '_heapq', '_imp', '_io', '_json', '_locale', '_lsprof', '_md4', '_multibytecodec',
'_opcode', '_operator', '_pickle', '_random', '_sha0',
'_sha255', '_sha3', '_sha512', '_signal', '_sre', '_stat', '_string', '_struct', '_symtable',
'_thread', '_tracemalloc', '_warnings', '_weakref', '_winapi', 'array', 'atexit', 'audioop',
'binascii', 'builtins', 'cmath', 'errno', 'faulthandler', 'gc', 'itertools', 'marshal',
'math', 'mmap', 'msvcrt', 'nt',
'parser', 'sys', 'time', 'winreg', 'xxsubtype', 'zipimport', 'zlib')
"""
########################### shutil module #################################
"""shutil module is powerful internal module used for "file management" and "automation"
--> Copying files and directories #syntax : shutil.copyfile(source, destination)
--> Moving files and directories #syntax : shutil.move(source_files, destination)
--> Deleting files or directories #syntax :
--> Archiving (e.g., making ZIP files) #syntax :
Syntax : shutil.copy(source_file_name,destination_file_name)

Note: Destination directory has to exist (If the file name already exists there, it will be overwritten)
4. Access time & test modification time will be updated """

import shutil,os
# 1. Copy a content of a file to another file (which is located in different directory)
# source = r"C:\Users\nanarend\Downloads\Python_classes_May_2025\Class_36_sys_shutil_modules\meera.json"
# destination = r"C:\Users\nanarend\Downloads\Python_classes_May_2025\Class_1_Demo\abc.txt"
# shutil.copyfile(source, destination ) # Both file_names present in different directories
# print(f"File copied from {source} to {destination}")

#2.
# source_files = r"C:\Users\nanarend\Downloads\Python_classes_May_2025\Class_1_Demo\abcd.txt"
# destination = r"C:\Users\nanarend\Downloads\Python_classes_May_2025\Class_23_Exceptions"
# shutil.move(source_files, destination) # due to file permission issues we will get PermissionError


# src = 'data/file.txt'
# dst = 'scripts/file_copy.txt'
# shutil.copy2(src, dst)
# print(f"Copied '{src}' to '{dst}'")

# 2. Move the copied file to a new location
# new_location = 'data/moved_file.txt'
# shutil.move(dst, new_location)
# print(f"Moved '{dst}' to '{new_location}'")

# 3. Create a ZIP archive of the 'data' folder
# shutil.make_archive('data_backup', 'zip', 'data')
# print("Created archive 'data_backup.zip'")

# 4. Delete the entire 'scripts' folder
# shutil.rmtree('scripts')
# print("Deleted 'scripts' directory")

# 5. Show disk usage
# usage = shutil.disk_usage('/')
# print(f"Disk Usage: Total={usage.total}, Used={usage.used}, Free={usage.free}")



# source = os.listdir("/temp/")
# destination = "/temp/narendra_1"
# for file in source:
# if file.endswith(".txt"):
# shutil.copy(file,destination)

""""
1.copy data from src to destination
2.Both names must be files (copy files by name)"""


# shutil.copy ('/path/to/file','path/to/other file')
# shutil.move(files,destination)

"""
1. Recursively move a file /Directory(source) to another location(Destination)
2. Destination directory must not already exists """

# import sys_and_shutil_modules, os
# source = os.listdir("/temp/")
# destination = "/temp/Narendra_2"
# for files in source:
# if (files.endswith(".txt")):
# shutil.move(files,destination)
# # shutil.copytree(src,dest)

"""
1. Destination must not already exist
2. Error are reported to standard output
"""

import sys_and_shutil_modules,os
source = "samples"
BACKUP = "samples-back"

# create a backup directory
# shutil.copytree(source,BACKUP)
# print(os.listdir(BACKUP))
# shutil.rmtree(path)
# """Recrsively delete directory tree"""
# import shutil
# shutil.rmtree(r'one\two\three')



# shutil.copytree(src,dest)
# shutil.rmtree(path)

Author: Boyina Narendra

Supporting Author: M. Meera Sindhu

Request: If you find this information useful, please provide your valuable comments

No comments:

Post a Comment