Monday, April 21, 2025

Compress Multiple Files to Individual ZIP Files in One Go

import zipfile, os

os.chdir('c:\\Python38-64\\include')

for x in os.listdir():
    if x.endswith('.h'):
        handle = zipfile.ZipFile(x + '.zip', 'w')
        handle.write(x, compress_type = zipfile.ZIP_DEFLATED)
        handle.close()

This Python code uses the zipfile and os modules to compress all the header files (.h) in the c:\Python38-64\include directory and its subdirectories into individual ZIP files.

The os module is used to change the current working directory to c:\Python38-64\include using the os.chdir() method. Then, the os.listdir() method is used to get a list of all files and directories in the current directory.

The if statement is used to filter out only the files that end with .h. For each header file, a new ZIP file is created with the same filename as the header file but with a .zip extension using the zipfile.ZipFile() method. The compress_type argument is set to zipfile.ZIP_DEFLATED to use the ZIP file compression method.

The header file is added to the ZIP file using the write() method of the ZIP file handle. Finally, the ZIP file is closed using the close() method of the ZIP file handle.

Compress Multiple Files in One ZIP File - Python

import zipfile, os

handle = zipfile.ZipFile('ALL-PYD.zip', 'w')

os.chdir('c:\\Python38-64\\DLLs')

for x in os.listdir():
    if x.endswith('.pyd'):
        handle.write(x, compress_type = zipfile.ZIP_DEFLATED)

handle.close()

This Python script creates a new ZIP archive file named ALL-PYD.zip in the current working directory. It then changes the current working directory to c:\Python38-64\DLLs. For each file in the directory that has a .pyd extension, it adds the file to the ZIP archive using the write() method of the ZipFile object. The files are compressed using the ZIP_DEFLATED compression method. Finally, it closes the ZIP archive.

So, this script is used to create a compressed archive of all the files in the c:\Python38-64\DLLs directory with a .pyd extension, making it easier to transfer or distribute the files.

How to Create a ZIP File - Python

import zipfile

target = 'zip-test.py'

handle = zipfile.ZipFile('BACKUP.zip', 'w')

handle.write(target, compress_type = zipfile.ZIP_DEFLATED)

handle.close()

This Python script creates a new Zip archive named "BACKUP.zip" and adds a file named "zip-test.py" to it. The file is compressed using the "DEFLATED" compression method, which is one of the standard compression methods supported by the Zip format.

The first line imports the "zipfile" module, which provides classes and functions for working with Zip archives.

The second line creates a new Zip archive named "BACKUP.zip" in write mode ('w') using the "ZipFile" class from the "zipfile" module.

The third line adds the file named "zip-test.py" to the Zip archive using the "write" method of the ZipFile object. The "compress_type" parameter specifies the compression method to use, in this case "ZIP_DEFLATED".

The fourth line closes the Zip archive using the "close" method of the ZipFile object.

Extract Files with Specific Extension from ZIP File

import zipfile

target = 'OLD-BACKUP.zip'

handle = zipfile.ZipFile(target)

for x in handle.namelist():
    if x.endswith('.cat'):
        handle.extract(x, 'UNPACK-SPECIFIC')

This Python code uses the zipfile module to extract specific files from a ZIP archive called 'OLD-BACKUP.zip' that have the file extension .cat to a specific directory 'UNPACK-SPECIFIC'.

The zipfile.ZipFile method is used to create a handle to the specified ZIP file. The namelist method is then called on the handle to get a list of all the filenames in the ZIP archive. A for loop is then used to iterate through each filename in the list.

For each filename, an if statement checks if the filename ends with the string '.cat'. If it does, the extract method is called on the handle to extract that specific file to the directory 'UNPACK-SPECIFIC'.

Note that if the target directory already contains a file with the same name as the file being extracted, it will be overwritten.

How to Unzip a File - Python

import zipfile

target = 'OLD-BACKUP.zip'

handle = zipfile.ZipFile(target)

handle.extractall('c:\\Python38-64\\UNPACKED')

handle.close()

This Python code uses the zipfile module to extract all files from a ZIP archive called 'OLD-BACKUP.zip' to a target directory 'c:\Python38-64\UNPACKED'.

The zipfile.ZipFile method is used to create a handle to the specified ZIP file. The extractall method is then called on the handle to extract all files in the ZIP archive to the target directory. Finally, the handle.close() method is called to close the handle to the ZIP file.

Note that if any of the files being extracted already exist in the target directory, they will be overwritten.

File Size Before and After ZIP Compression - Python

import zipfile

target = 'OLD-BACKUP.zip'

handle = zipfile.ZipFile(target)

listing = handle.namelist()

for x in listing:
    res = handle.getinfo(x)
    print('-' * 79)
    print('Filename: ', res.filename)
    print('Before: ', res.file_size)
    print('After: ', res.compress_size)

This code imports the zipfile module and then opens a ZIP file named 'OLD-BACKUP.zip' using the ZipFile() function from the zipfile module.

The code then uses the namelist() method to get a list of all the files and directories present in the ZIP archive and stores the list in a variable named listing.

The code then loops through the listing variable and for each item in the list:

  1. It gets information about the item in the ZIP archive using the getinfo() method of the ZipFile object handle and stores the information in a variable named res.
  2. It then prints a separator of 79 dashes to visually separate the output for each item in the archive.
  3. It prints the filename of the item in the archive, obtained from the filename attribute of the ZipInfo object res.
  4. It prints the size of the file before compression, obtained from the file_size attribute of the ZipInfo object res.
  5. It prints the size of the file after compression, obtained from the compress_size attribute of the ZipInfo object res.

So, this code essentially displays the filenames, original sizes, and compressed sizes of all the files and directories present in the 'OLD-BACKUP.zip' archive.

List ALL Files in ZIP Archives Without Decompresion - Python

import zipfile

target = 'OLD-BACKUP.zip'

handle = zipfile.ZipFile(target)

for x in handle.namelist():
    print(x)

This script imports the zipfile module and then opens the file named 'OLD-BACKUP.zip' using the ZipFile() function from the zipfile module.

Once the ZIP file is opened, the code loops through all the files and directories present in the ZIP archive using the namelist() method. For each file and directory, it prints the name to the console using the print() function.

This code essentially lists all the files and directories present in the 'OLD-BACKUP.zip' archive. 

OLD-BACKUP/
OLD-BACKUP/py.ico
OLD-BACKUP/pyc.ico
OLD-BACKUP/pyd.ico
OLD-BACKUP/python_lib.cat
OLD-BACKUP/python_tools.cat
>>> 

The output shows the names of all the files and directories that are present in the root directory of the 'OLD-BACKUP.zip' archive, including:

  • A directory named 'OLD-BACKUP/'.
  • Four .ico files named 'py.ico', 'pyc.ico', 'pyd.ico', and 'python_lib.cat'.
  • A file named 'python_tools.cat'
import zipfile

target = 'OLD-BACKUP.zip'

handle = zipfile.ZipFile(target)

listing = handle.namelist()

file = open('zip-listing.txt', 'w')

for x in listing:
    file.write(x + '\n')

file.close()

This code imports the zipfile module and then opens a ZIP file named 'OLD-BACKUP.zip' using the ZipFile() function from the zipfile module.

The code then uses the namelist() method to get a list of all the files and directories present in the ZIP archive and stores the list in a variable named listing.

Next, the code creates a new file named 'zip-listing.txt' in write mode using the open() function with the 'w' argument. This file will be used to store the listing of all the files and directories present in the ZIP archive.

The code then loops through the listing variable and writes the name of each file and directory to the 'zip-listing.txt' file using the write() method of the file object. The '\n' at the end of each name ensures that each name is written on a new line.

Finally, the code closes the 'zip-listing.txt' file using the close() method of the file object.

This code essentially creates a text file named 'zip-listing.txt' that contains a listing of all the files and directories present in the 'OLD-BACKUP.zip' archive.

Create MySQL Table - One or More - Python

import mysql.connector

db_handle = mysql.connector.connect(
    host = 'localhost',
    user = 'root',
    passwd = '12345',
    database = '192_168_0_103'
)

print(db_handle)

kursor = db_handle.cursor()

kursor.execute("CREATE TABLE table_1 (name VARCHAR(255), address VARCHAR(255))")

kursor.execute("CREATE TABLE table_2 (name VARCHAR(255), address VARCHAR(255))")

This code imports two modules, "mysql.connector" and "time".

The "mysql.connector" module is used to connect to a MySQL database server, and execute queries on it. It is a Python library that provides a standardized way to interact with the MySQL database.

The code establishes a connection to the MySQL database server running on "localhost" with the username "root" and password "12345", and specifies the database that we want to connect to - '192_168_0_103'. It then creates a cursor object to execute queries on the database.

The code then executes two queries using the cursor object:

  • The first query creates a new table called "table_1" with two columns: "name" and "address". The data type for both columns is "VARCHAR(255)", which means that each column can store a string of up to 255 characters.

  • The second query creates another new table called "table_2" with the same columns ("name" and "address") and data type as the first table.

Both queries are executed using the cursor object, and the tables are created in the currently connected database - '192_168_0_103'.

After executing both queries, the code does not perform any other action and terminates. 

mysql> use 192_168_0_103;
Database changed
mysql> show tables;
+-------------------------+
| Tables_in_192_168_0_103 |
+-------------------------+
| table_1                 |
| table_2                 |
+-------------------------+
2 rows in set (0.00 sec)

mysql> describe table_1;
+---------+--------------+------+-----+---------+-------+
| Field   | Type         | Null | Key | Default | Extra |
+---------+--------------+------+-----+---------+-------+
| name    | varchar(255) | YES  |     | NULL    |       |
| address | varchar(255) | YES  |     | NULL    |       |
+---------+--------------+------+-----+---------+-------+
2 rows in set (0.05 sec)

mysql>

The first command "use 192_168_0_103;" switches to the database named '192_168_0_103', making it the active database for subsequent queries.

The second command "show tables;" lists all the tables that are present in the currently active database. In this case, there are two tables in the '192_168_0_103' database - "table_1" and "table_2".

The third command "describe table_1;" displays the structure of the "table_1" table. It shows the names of the columns and their data types. In this case, the table has two columns named "name" and "address", both of type "VARCHAR(255)".

The output of each command is shown along with the time taken to execute the command, in seconds.

Create MySQL Databases from .TXT File - Python

import mysql.connector, time

db_handle = mysql.connector.connect(
    host = 'localhost',
    user = 'root',
    passwd = '12345'
)

print(db_handle)

kursor = db_handle.cursor()

names = []

file = open("database_names.txt", 'r')

for x in file.readlines():
    names.append(x)

for y in names:
    kursor.execute("CREATE DATABASE " + y)
    time.sleep(2)

file.close()

This code imports two modules, "mysql.connector" and "time".

The "mysql.connector" module is used to connect to a MySQL database server, and execute queries on it. It is a Python library that provides a standardized way to interact with the MySQL database.

The "time" module is used to introduce a delay in the execution of the code. In this particular code, it is used to introduce a delay of 2 seconds after creating each database.

The code establishes a connection to the MySQL database server running on "localhost" with the username "root" and password "12345". It then creates a cursor object to execute queries on the database.

It reads the names of databases from the file "database_names.txt" and stores them in a list called "names".

Then it uses a loop to execute a "CREATE DATABASE" query for each name in the "names" list, using the cursor object. The time.sleep(2) function is used to introduce a delay of 2 seconds between each database creation.

Finally, it closes the file. 

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| 192_168_0_103      |
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| test_1             |
| test_2             |
| test_3             |
+--------------------+
8 rows in set (0.00 sec)

mysql>

This is the output of the MySQL command "show databases". It displays a list of all the databases that are currently present on the MySQL server.

In this case, there are 8 databases in the list.

The "information_schema", "mysql", "performance_schema", and "sys" databases are default databases that are created when MySQL is installed.

Last 3 databases ("test_1", "test_2", and "test_3") are created by us, based on their names from .txt file.

Create Multiple MySQL Databases Automatically - Python

import mysql.connector, time

db_handle = mysql.connector.connect(
    host = 'localhost',
    user = 'root',
    passwd = '12345'
)

print(db_handle)

kursor = db_handle.cursor()

bases = ["slavoj", "zizek", "jordan", "peterson"]

for x in bases:
    kursor.execute("CREATE DATABASE " + x)
    time.sleep(2)

This script creates multiple databases in a MySQL server using Python with the mysql-connector package and the time module.

First, it establishes a connection to the MySQL server with the specified host, user, and password using the mysql.connector.connect() function, and then prints the handle to the database.

Then, it creates a cursor object using the cursor() method of the database handle, which is used to execute SQL statements.

Next, it defines a list of database names to be created, bases.

The script then loops over the list of database names, and for each name, it executes a SQL CREATE DATABASE statement with the corresponding name using the execute() method of the cursor object. It also includes a 2-second delay between each database creation using the time.sleep() function.

Our script will create four databases with the names "slavoj", "zizek", "jordan", and "peterson".

Result: 

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| 192_168_0_103      |
| information_schema |
| jordan             |
| mysql              |
| performance_schema |
| peterson           |
| slavoj             |
| sys                |
| test_dba           |
| zizek              |
+--------------------+
10 rows in set (0.00 sec)

mysql>

MySQL Create Database - Python

You are strongly advised to check corresponding YouTube video at the end of this article.

MySQL Connector is a library that allows Python programs to communicate with a MySQL database. It is used to establish a connection between Python applications and MySQL server to perform various operations such as inserting, deleting, updating, and retrieving data from a database.

In simple terms, MySQL Connector is a bridge between Python and MySQL server that allows Python developers to create, read, update, and delete data from MySQL databases using Python code.

MySQL Connector is important because it allows developers to integrate MySQL databases with their Python applications seamlessly. Without MySQL Connector, developers would have to write their own SQL code to interact with a MySQL database, which would be time-consuming and error-prone. By using MySQL Connector, developers can save time and focus on building their applications rather than writing low-level database code. 

C:\>pip install mysql-connector-python
Collecting mysql-connector-python
  Downloading mysql_connector_python-8.0.20-py2.py3-none-any.whl (358 kB)
     |████████████████████████████████| 358 kB 3.3 MB/s
Collecting protobuf>=3.0.0
  Downloading protobuf-3.12.2-py2.py3-none-any.whl (443 kB)
     |████████████████████████████████| 443 kB 6.8 MB/s
Requirement already satisfied: setuptools in c:\python38-64\lib\....
Requirement already satisfied: six>=1.9 in c:\python38-64\lib\....)
Installing collected packages: protobuf, mysql-connector-python
Successfully installed mysql-connector-python-8.0.20 protobuf-3.12.2

C:\>

These commands are used to install the Python package "mysql-connector-python" using pip.

The last line "Successfully installed mysql-connector-python-8.0.20 protobuf-3.12.2" confirms that both packages have been installed successfully. 


import mysql.connector

db_handle = mysql.connector.connect(
    host = 'localhost',
    user = 'root',
    passwd = '12345'
)

print(db_handle)

kursor = db_handle.cursor()
kursor.execute("CREATE DATABASE test_dba")

This script uses the mysql.connector Python library to connect to a MySQL database server running on the same machine at the default port, using the username 'root' and password '12345'. It then creates a new database named 'test_dba' using a SQL command executed via a cursor object.

Here's what each line does:

  1. import mysql.connector: imports the mysql.connector module, which provides a Python interface for working with MySQL databases.

  2. db_handle = mysql.connector.connect(host='localhost', user='root', passwd='12345'): creates a connection object named db_handle using the connect() method of the mysql.connector module. The host, user, and passwd parameters specify the server name, username, and password used to connect to the database.

  3. print(db_handle): prints the connection object to the console, which should output a message indicating that the connection was successful.

  4. kursor = db_handle.cursor(): creates a cursor object named kursor using the cursor() method of the db_handle object. Cursors are used to execute SQL commands and retrieve data from the database.

  5. kursor.execute("CREATE DATABASE test_dba"): executes a SQL command to create a new database named test_dba. The execute() method of the cursor object is used to execute the command.

Note that this script assumes that the MySQL server is already installed and running on the local machine, and that the root user has sufficient privileges to create new databases. 

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| 192_168_0_103      |
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| test_dba           |
+--------------------+
6 rows in set (0.00 sec)

mysql>

This is a MySQL report showing the result of the command "show databases;" executed in the MySQL command-line interface.

The output shows a table with the list of databases available in the MySQL server. Each row represents a database, and the only column in the table is named "Database".

The report shows that there are six databases available in the MySQL server:

  • 192_168_0_103
  • information_schema
  • mysql
  • performance_schema
  • sys
  • test_dba

The last database is "test_dba", which means that our script works just fine.

How to Create Virtual Environment and Django Project

C:\Users\user>python -m venv DJANGO_PROJECT

C:\Users\user>cd DJANGO_PROJECT

C:\Users\user\DJANGO_PROJECT>cd Scripts

C:\Users\user\DJANGO_PROJECT\Scripts>activate

(DJANGO_PROJECT) C:\Users\user\DJANGO_PROJECT\Scripts>

We have created a new virtual environment named "DJANGO_PROJECT" using the venv module.

Then we navigated to the virtual environment's Scripts directory and activated the environment using the activate command. The (DJANGO_PROJECT) prefix in the command prompt indicates that the virtual environment is currently active.

Any Python packages installed while the virtual environment is active will be isolated from the global Python environment and will only be available within the virtual environment. 

(DJANGO_PROJECT) C:\Users\user\DJANGO_PROJECT\Scripts>pip install django
Collecting django
  Using cached https://files.pythonhosted.org/packages/..../Django-3.0.7-py3-none-any.whl
Collecting pytz (from django)
  Using cached https://files.pythonhosted.org/packages/..../pytz-2020.1-py2.py3-none-any.whl
Collecting sqlparse>=0.2.2 (from django)
  Using cached https://files.pythonhosted.org/packages/..../sqlparse-0.3.1-py2.py3-none-any.whl
Collecting asgiref~=3.2 (from django)
  Using cached https://files.pythonhosted.org/packages/..../asgiref-3.2.10-py3-none-any.whl
Installing collected packages: pytz, sqlparse, asgiref, django
Successfully installed asgiref-3.2.10 django-3.0.7 pytz-2020.1 sqlparse-0.3.1
WARNING: You are using pip version 19.2.3, however version 20.1.1 is available.
You should consider upgrading via the 'python -m pip install --upgrade pip' command.

(DJANGO_PROJECT) C:\Users\user\DJANGO_PROJECT\Scripts>

The command pip install django installs the Django package in the virtual environment. The output shows that the package and its dependencies (pytz, sqlparse, and asgiref) were successfully installed. 

(DJANGO_PROJECT) C:\Users\user\DJANGO_PROJECT\Scripts>django-admin startproject DJANGO_PROJECT

(DJANGO_PROJECT) C:\Users\user\DJANGO_PROJECT\Scripts>cd DJANGO_PROJECT

(DJANGO_PROJECT) C:\Users\user\DJANGO_PROJECT\Scripts\DJANGO_PROJECT>python manage.py runserver
Watching for file changes with StatReloader
Performing system checks...

System check identified no issues (0 silenced).

You have 17 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions.
Run 'python manage.py migrate' to apply them.
June 21, 2020 - 21:59:50
Django version 3.0.7, using settings 'DJANGO_PROJECT.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.
[21/Jun/2020 22:00:09] "GET / HTTP/1.1" 200 16351
[21/Jun/2020 22:00:09] "GET /static/admin/css/fonts.css HTTP/1.1" 200 423
[21/Jun/2020 22:00:09] "GET /static/admin/fonts/Roboto-Bold-webfont.woff HTTP/1.1" 200 86184
[21/Jun/2020 22:00:09] "GET /static/admin/fonts/Roboto-Regular-webfont.woff HTTP/1.1" 200 85876
[21/Jun/2020 22:00:09] "GET /static/admin/fonts/Roboto-Light-webfont.woff HTTP/1.1" 200 85692
Not Found: /favicon.ico
[21/Jun/2020 22:00:09] "GET /favicon.ico HTTP/1.1" 404 1980

This code creates a new Django project called "DJANGO_PROJECT" and runs the development server.

  • django-admin startproject DJANGO_PROJECT creates a new Django project in a new directory called "DJANGO_PROJECT".
  • cd DJANGO_PROJECT navigates to the newly created directory.
  • python manage.py runserver starts the development server on the default port 8000 and displays some information in the console about the server status, including the version of Django being used and the settings file being used. It also warns that there are 17 unapplied migrations that should be run, and provides instructions on how to run them. The server then listens for incoming HTTP requests and logs information about each request it receives.

How to Upgrade PIP in Windows - using Command Prompt

You can upgrade pip in Windows by following these steps:

  1. Open the Command Prompt as an administrator. To do this, search for "Command Prompt" in the Start menu, right-click on it, and select "Run as administrator".

  2. Enter the following command: 

    python -m pip install --upgrade pip
    
  3. Press Enter.

  4. Wait for the process to complete. This may take a few minutes.

  5. Once the upgrade is complete, you should see a message confirming that the upgrade was successful. 

Microsoft Windows [Version 10.0.18363.900]
(c) 2019 Microsoft Corporation. All rights reserved.

C:\Users\user>python -m pip install --upgrade pip
Requirement already up-to-date: pip in c:\python38-64\lib\site-packages (20.1.1)

C:\Users\user>

It is generally a good idea to upgrade pip to the latest version as it provides bug fixes, security updates, and new features.

How to Create Python Virtual Environment - venv Tutorial

You are strongly advised to check corresponding YouTube video at the end of this article.

Using a virtual environment, like venv in Python, is a smart idea for a few reasons:

  1. Isolation: Virtual environments allow you to create an isolated environment for your Python project, separate from the system's Python installation and other projects. This isolation prevents conflicts between different project dependencies and ensures that you have a clean and consistent environment for your project.

  2. Reproducibility: Virtual environments allow you to specify the exact Python version and package versions required for your project. This makes it easier to reproduce your project's environment on another machine or to deploy your project to a production environment.

  3. Security: Using virtual environments can help prevent security vulnerabilities. Since your project's dependencies are installed in an isolated environment, any vulnerabilities or security issues with those dependencies won't affect other projects or the system as a whole.

Using a virtual environment provides a clean and consistent environment for your Python project, with all the required dependencies, while avoiding potential conflicts and security issues. 

C:\>python -m venv test_stuff

C:\>cd test_stuff

C:\test_stuff>dir
 Volume in drive C is New Volume
 Volume Serial Number is B082-0A47

 Directory of C:\test_stuff

06/21/2020  09:08 PM              .
06/21/2020  09:08 PM              ..
06/21/2020  09:08 PM              Include
06/21/2020  09:08 PM              Lib
06/21/2020  09:08 PM                78 pyvenv.cfg
06/21/2020  09:09 PM              Scripts
               1 File(s)             78 bytes
               5 Dir(s)  78,625,415,168 bytes free

C:\test_stuff>

This is a sequence of commands that create a virtual environment in Python:

  • python -m venv test_stuff: This command creates a virtual environment named "test_stuff" using the built-in venv module of Python. The -m flag tells Python to run a module as a script, and venv is the name of the module that creates virtual environments.

  • cd test_stuff: This command changes the current working directory to the newly created virtual environment.

  • dir: This command lists the contents of the current working directory. Since we're now in the virtual environment directory, we see the folders and files that make up the virtual environment.

The Include and Lib directories are where the standard library and other installed packages are stored. The pyvenv.cfg file contains configuration options for the virtual environment, and the Scripts directory contains scripts for activating and deactivating the virtual environment, as well as running Python scripts within the environment. 


C:\test_stuff>cd Scripts

C:\test_stuff\Scripts>activate

(test_stuff) C:\test_stuff\Scripts>

The activate script is used to activate the virtual environment.

Once we run the activate script, we will notice that the name of the virtual environment is added to the command prompt, indicating that the virtual environment is active. In our example, the virtual environment named test_stuff is active and the command prompt shows (test_stuff) at the beginning of the prompt. Any packages installed while the virtual environment is active will be installed only in that virtual environment and will not affect the global Python environment or other virtual environments. 

(test_stuff) C:\test_stuff\Scripts>python -m pip install --upgrade pip
Collecting pip
  Using cached https://files.pythonhosted.org/packages/.....
Installing collected packages: pip
  Found existing installation: pip 19.2.3
    Uninstalling pip-19.2.3:
      Successfully uninstalled pip-19.2.3
Successfully installed pip-20.1.1

(test_stuff) C:\test_stuff\Scripts>

This command upgrades the "pip" package in the virtual environment to the latest version using pip itself. The output shows that pip was successfully upgraded from version 19.2.3 to version 20.1.1. 

(test_stuff) C:\test_stuff\Scripts>pip install django
Collecting django
  Downloading Django-3.0.7-py3-none-any.whl (7.5 MB)
     |████████████████████████████████| 7.5 MB 97 kB/s
Collecting pytz
  Downloading pytz-2020.1-py2.py3-none-any.whl (510 kB)
     |████████████████████████████████| 510 kB ...
Collecting asgiref~=3.2
  Downloading asgiref-3.2.10-py3-none-any.whl (19 kB)
Collecting sqlparse>=0.2.2
  Downloading sqlparse-0.3.1-py2.py3-none-any.whl (40 kB)
     |████████████████████████████████| 40 kB 427 kB/s
Installing collected packages: pytz, asgiref, sqlparse, django
Successfully installed asgiref-3.2.10 django-3.0.7 pytz-2020.1 sqlparse-0.3.1

(test_stuff) C:\test_stuff\Scripts>

This code installs the Django web framework using pip. Pip installs all the dependencies required by Django such as pytz, asgiref, and sqlparse. Once the installation is complete, the Django web framework is now ready to be used in the virtual environment. 


(test_stuff) C:\test_stuff\Scripts>deactivate
C:\test_stuff\Scripts>

This code deactivates the virtual environment created earlier using the command "python -m venv test_stuff" and returns the command prompt to the default system environment. The virtual environment is indicated by the prefix "(test_stuff)" in the command prompt.

The "deactivate" command is a built-in command provided by the virtual environment package in Python. When this command is run, it disables the virtual environment and restores the default system environment.

Get a List of All Subdirectories in the Main Directory

import os

target = 'c:\\Python38-64'

report = open('specific-ext.txt', 'w')

for root, dirname, files in os.walk(target):
    for x in dirname:
        print(x)

This code recursively traverses through the target directory (in this case, c:\\Python38-64), and for each subdirectory in the target, it prints out the name of the directory.

The os.walk() function generates the file names in a directory tree by walking the tree either top-down or bottom-up. In this code, it is used to walk through the target directory and all its subdirectories.

The function returns a tuple with three values for each directory it visits during the traversal: the path to the directory, a list of its subdirectories, and a list of its files. In this code, we only loop through the subdirectories and print their names using a for loop.

Note that since we are only interested in the subdirectory names and not the files, we only loop through the dirname variable and not the files variable. 

import os

target = 'c:\\Python38-64'

report = open('specific-ext.txt', 'w')

for root, dirname, files in os.walk(target):
    for x in dirname:
        print(root + x)

The for loop iterates over the directories found in each subdirectory, and the path of the current subdirectory is concatenated with the name of the directory to obtain the full path. Finally, the full path is printed to the console.

Tkinter Introduction - Top Widget, Method, Button

First, let's make shure that our tkinter module is working ok with simple  for loop that will spawn 5 instances of blank Tk window .  ...