My QA Projects

QA Projects I was involded.

View on GitHub

File Operations

os.path module

  1. String-Based Operations: The os.path module primarily works with paths represented as strings.

  2. Platform Independence: It provides functions for common path-related operations like joining paths, splitting paths, getting the basename, dirname, and more. These functions are designed to work across different operating systems (Windows, Unix, macOS).

  3. Legacy: os.path has been around for a long time and is considered part of the legacy file path handling system in Python.

import os
os.path.exists('oops.txt')
# Output
# True
os.path.exists('./oops.txt')
# Output
# True
os.path.exists('.')
# Output
# True
os.path.exists('..')
# Output
# True

# Joining paths
path = os.path.join('folder', 'file.txt')

# Splitting paths
dirname, filename = os.path.split(path)

pathlib module

  1. Object-Oriented Approach: The pathlib module provides an object-oriented approach for working with paths. It represents paths as objects.

  2. Rich API: It offers a rich API for path manipulation, making code more readable and expressive. Path objects have methods for various path operations.

  3. Pure Path: Path objects in pathlib can represent both absolute and relative paths. They are platform-independent and provide consistent behavior across different operating systems.

  4. Modern: pathlib is a more modern and preferred way of handling paths in Python, especially for new code.

from pathlib import Path

# Creating a path object
path = Path('folder') / 'file.txt'

# Getting parent directory
parent_dir = path.parent

Key Differences: Syntax: os.path uses string-based operations, while pathlib uses object-oriented syntax.

Object-Oriented vs. Functional: pathlib provides an object-oriented interface, making code more readable and allowing for method chaining. os.path uses functional programming style with standalone functions.

Ease of Use: pathlib is often considered more intuitive and easier to use for path manipulation, especially in modern Python code.

In summary, while both os.path and pathlib serve the purpose of working with filesystem paths,


isfile() [to check type]

wether a name refers to a file, directory, or symbolic link

name='oops.txt'
os.path.isfile(name)
# Output
# True

isdir() [to determine directory]

name='oops.txt'
os.path.isdir('.')
# Output
# True
os.path.isabs(name)
# Output
# False
os.path.isabs('/big/fake/name')
# Output
# True

copy() [for Copying]

import shutil
shutil.copy('oops.txt, ohno.txt')

rename() [to change file name]

import os
os.rename('nm.txt','name.txt')

Syntax

os.link('target.txt', 'link.txt')
os.isfile('link.txt')
# Output
# True

os.islink('link.txt')
# Output
# False

Syntax

os.symlink(target, link)
import os
os.symlink('/path/to/target', '/path/to/link')
import os
is_symlink = os.path.islink('/path/to/link')

chmod() [to change permission]

os.chmod('oops.txt', 0o400)

# OR

import stat
os.chmod('oops.txt', stat.S_IRUSR)

chown() [to change ownership]

uid = 5
gid = 22
os.chown('oops', uid,gid)

remove() [to delete a file]

import os
os.remove("oops.txt")
os.path.exists("oops.txt")
# OUTPUT
# False

Dictionary Operations

mkdir()

import os
os.mkdir('poems')
os.path.exists("poems")
# OUTPUT
# True

rmdir()

os.rmdir('poems')
os.path.exists("poems")
# OUTPUT
# False

listdir()

import os
os.mkdir('poems')

#get a list of its content
os.listdir('poems')
[]

# create subdirectory
os.mkdir('poems/goethe')
os.listdir('poems')
['goethe']

# create a file in this subdirectory
fout = open('poems/goethe/faust', 'wt')
fout.write(''' Two souls, alas, are housed within my breast,
And each will wrestle for the mastery there.''')
fout.close()

os.listdir('poems/goethe')
# OUTPUT
# ['faust']

chdir()

globdir() [for list matching]

import glob
glob.glob('f*')
# OUTPUT
# ['faust']

# any two letter files
import glob
glob.glob('??')
# OUTPUT
# ['']

Pathnames

abspath()

os.path.abspath('oops.txt')
# OUTPUT
# [/usr/mike/oops.txt]
os.path.abspath('oops.txt')
# Output
'/usr/mike/oops.txt'

os.path.join() [to build a pathname]

import os
my_file = os.path.join("eek", "urk")
my_file = os.path.join(my_file, "snort.txt")

> my_file
'eek/urk/snort.txt'

=> need to research this, no idea what this is about

pathlib

import os


From the exercises in the book

List the files in current dir

import os
os.listdir('.')
['bears', 'lions', 'tigers']

List the files in parent dir

import os
os.listdir('..')

assign the string to the variabl test1, ane write test1 to a file called test.txt

test1 = 'this is a test string'
outfile = open('test.txt', 'wt')
outfile.write(test1)
outfile.close

# alternitavely
with open('test.txt', 'wt') as outfile:
  outfile.write(test1)
import os
if os.path.exists("demofile.txt"):
  os.remove("demofile.txt")
else:
  print("The file does not exist")

back