Python >> Tips and Tricks 3

2022-08-13 Python

Table of Contents

This tutorial will explain how to use virtual environment, absolute import and relative import, get absolute path of file, use argparse module, use set(), use range, get all files in a folder.

Python

Python how to use virtual environment

What is Python virtual environment

Python virtual environment is a isolated environment where you can install and run Python program.

How to install Python virtual environment

# Install Python virtual environment
pip install virtualenv

How to create Python virtual environment

# Create Python virtual environment
virtualenv -p python3 myenv

How to activate Python virtual environment

# Activate Python virtual environment
source myenv/bin/activate

How to deactivate Python virtual environment

# Deactivate Python virtual environment
deactivate

Python how to absolute import and relative import

Absolute import

# absolute import
from pkg1.pkg2.mymodule1 import func2

Relative import

# relative import
from .mymodule2 import func2

Python how to get absolute path of file

Use os.path module to get absolute path of file

# Get absolute path of file
import os

print(os.path.abspath('test.txt'))

d:\myfolder\test.txt

Use pathlib module to get absolute path of file

# Get absolute path of file
import pathlib

print(pathlib.Path('test.txt').resolve())

d:\myfolder\test.txt

Use path module to get absolute path of file

# Get absolute path of file
import path

print(path.Path('test.txt').abspath())

Python how to use argparse module

Create command line argument parser

# Create command line argument parser

import argparse

parser = argparse.ArgumentParser(description='This is a command line argument parser')
parser.add_argument('-a', '--arg1', help='This is argument 1')
parser.add_argument('-b', '--arg2', help='This is argument 2')
parser.add_argument('-c', '--arg3', help='This is argument 3')
args = parser.parse_args()

print(args.arg1)
print(args.arg2)
print(args.arg3)
Save the code above to test.py

python test.py -a 1 -b 2 -c 3

1
2
3

Python how to use set

Create set

# Create set
my_set = set()
my_set.add(1)
my_set.add(2)
my_set.add(3)

print(my_set)

{1, 2, 3}

Add element to set

# Add element to set
my_set.add(4)

print(my_set)

{1, 2, 3, 4}

Remove element from set

# Remove element from set
my_set.remove(3)

print(my_set)

{1, 2, 4}

Use set() to remove duplicate element from list

# Use set() to remove duplicate element from list
my_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4, 5, 6, 7, 8, 9]
my_set = set(my_list)
print(my_set)

{1, 2, 3, 4, 5, 6, 7, 8, 9}

Python how to use range

Create range from 0 and loop it

# Create a range from 0 to 9 and loop it
my_range = range(10)

for i in my_range:
    print(i)
0
1
2
3
4
5
6
7
8
9

Create range from 1 and loop it

# Create range
my_range = range(1, 11)

for i in my_range:
    print(i)
1
2
3
4
5
6
7
8
9
10

Create range and loop it with step

# Create range
my_range = range(1, 11, 2)

for i in my_range:
    print(i)
1
3
5
7
9

Python how to get all files in a folder

Use os.walk to get all files in a folder

# Use os.walk to get all files in a folder
import os

for root, dirs, files in os.walk(r'D:\temp'):
    for file in files:
        print(os.path.join(root, file))

Python Tips and Tricks

Use glob module to get all files in a folder

# Use glob module to get all files in a folder
import glob

for file in glob.glob(r'D:\test\*.txt'):
    print(file)
D:\test\test2.txt
D:\test\test4.txt

Use os.listdir to get all files in a folder

# Use os.listdir to get all files in a folder
import os

for file in os.listdir(r'D:\test'):
    print(file)

Not only files but also folders

folder1
folder2
test2.txt
test4.txt

We can use isfile() to check if the file is a file or not

# Use os.listdir to get all files in a folder
import os

for file in os.listdir(r'D:\test'):
    if os.path.isfile(os.path.join(r'D:\test', file)):
        print(file)
test2.txt
test4.txt

Subscribe and be the FIRST reader of our latest articles

* indicates required

Contact us