Python

  • Python ZIP file with Example

    Python allows you to quickly create zip/tar archives.

    Following command will zip entire directory

    shutil.make_archive(output_filename, 'zip', dir_name)

    Following command gives you control on the files you want to archive

    ZipFile.write(filename)

    Here are the steps to create Zip File in Python

    Step 1) To create an archive file from Python, make sure you have your import statement correct and in order. Here the import statement for the archive is from shutil import make_archive

    Python OS Module, Shell Script Commands

    Code Explanation

    • Import make_archive class from module shutil
    • Use the split function to split out the directory and the file name from the path to the location of the text file (guru99)
    • Then we call the module “shutil.make_archive(“guru99 archive, “zip”, root_dir)” to create archive file, which will be in zip format
    • After then we pass in the root directory of things we want to be zipped up. So everything in the directory will be zipped
    • When you run the code, you can see the archive zip file is created on the right side of the panel.

    Step 2) Once your archive file is made, you can right-click on the file and select the O.S, and it will show your archive files in it as shown below

    Python OS Module, Shell Script Commands

    Now your archive.zip file will appear on your O.S (Windows Explorer)

    Python OS Module, Shell Script Commands

    Step 3) When you double-click on the file, you will see the list all the files in there.

    Python OS Module, Shell Script Commands

    Step 4) In Python we can have more control over archive since we can define which specific file to include under archive. In our case, we will include two files under archive “guru99.txt” and “guru99.txt.bak”.

    Python OS Module, Shell Script Commands

    Code Explanation

    • Import Zipfile class from zip file Python module. This module gives full control over creating zip files
    • We create a new Zipfile with name ( “testguru99.zip, “w”)
    • Creating a new Zipfile class, requires to pass in permission because it’s a file, so you need to write information into the file as newzip
    • We used variable “newzip” to refer to the zip file we created
    • Using the write function on the “newzip” variable, we add the files “guru99.txt” and “guru99.txt.bak” to the archive

    When you execute the code you can see the file is created on the right side of the panel with name “guru99.zip”

    Note: Here we don’t give any command to “close” the file like “newzip.close” because we use “With” scope lock, so when program falls outside of this scope the file will be cleaned up and is closed automatically.

    Step 5) When you -> right click on file (testguru99.zip) and -> select your O.S (Windows Explorer), it will show the archive files in the folder as shown below.

    Python OS Module, Shell Script Commands

    When you double click on file “testguru99.zip”, it will open another window, and this will show the files included in it.

    Python OS Module, Shell Script Commands

    Here is the complete code

  • Python Rename File and Directory using os.rename()

    โšก Smart Summary Python renames files and directories with os.rename(). Here you will learn os.rename(), os.renames(), shutil.move(), and pathlib Path.rename(), plus how to rename a directory and batch-rename multiple files with clear examples. โœ๏ธ os.rename(): os.rename(src, dst) renames a file or directory; the source must exist. ๐Ÿ—‚๏ธ os.renames(): os.renames() also creates missing destination folders automatically….

  • Copy File in Python: shutil.copy(), shutil.copystat() method

    โšก Smart Summary Python copies files with the shutil module. shutil.copy() duplicates a file’s contents, while shutil.copystat() also copies permissions, modification time, and metadata. Here you will learn both methods with a step-by-step example. ๐Ÿ“‹ shutil.copy(): shutil.copy(src, dst) copies a file’s contents to a new destination. ๐Ÿ—‚๏ธ shutil.copystat(): shutil.copystat(src, dst) also copies permissions, time, and…

  • Python Check if File Exists (with Examples)

    โšก Smart Summary Python lets you check whether a file or directory exists using the os.path module and pathlib. Here you will learn os.path.exists(), os.path.isfile(), os.path.isdir(), and pathlib.Path.exists(), each with a clear code example. โœ… exists(): os.path.exists(path) returns True if a file or directory exists. ๐Ÿ“„ isfile() / isdir(): These return True only for an…

  • Python Timer Function: Measure Elapsed Time with EXAMPLES

    Python provides a library that helps understand and read the time information in many ways. In Python, this library is termed the time module, and the module provides time and date objects to perform any time-related operation. Such operations do not manipulate timestamps or strings; instead, they manipulate objects. Moreover, the Python time module offers…

  • Python enumerate() Function: Loop, Tuple & String

    โšก Smart Summary Python enumerate() adds an automatic counter to any iterable, returning index-value pairs as you loop. Here you will learn its syntax, the optional start index, and how it works on lists, tuples, strings, and dictionaries. ๐Ÿ”ข What it does: Adds a counter to each item of an iterable, returning index-value pairs. โš™๏ธ…

End of content

End of content