python
#Working With Files in Python
#https://realpython.com/working-with-files-in-python/
#python3
import os
from pathlib import Path
import shutil
#Get filename from filepath
basename = os.path.basename(filepath)
basename_without_ext = os.path.splitext(os.path.basename(filepath))[0]
#File existence check
cwd = os.getcwd()
cwd_f = cwd + "/"+filename
if os.path.isfile(cwd_f) == True
#Read file
#https://dbader.org/blog/python-file-io
with open('data.txt', 'r') as f:
data = f.read()
print('context: {}'.format(data))
#File reading & editing
with open('data.txt', 'w') as f:
data = 'some data to be written to the file'
f.write(data)
#Get folder & file list
#Folder structure
# my_directory
# ├── file1.py
# ├── file2.csv
# ├── file3.txt
# ├── sub_dir
# │ ├── bar.py
# │ └── foo.py
# ├── sub_dir_b
# │ └── file4.txt
# └── sub_dir_c
# ├── config.py
# └── file5.txt
#Method 1
entries = os.listdir('my_directory')#entries is a list
for entry in entries:
print(entry)
#→['file1.py', 'file2.csv', 'file3.txt', 'sub_dir', 'sub_dir_b', 'sub_dir_c']
#Method 2 python 3.5 or later
with os.scandir('my_directory') as entries:#entries is an iterator
for entry in entries:
print(entry.name)
#Method 3 python 3.4 or later
entries = Path('my_directory')
for entry in entries.iterdir():
print(entry.name)
#Get list including subfolders
for dirpath, dirname, files in os.walk('.'):
print(f'Found directory: {dirpath}')
for file_name in files:
print(file_name)
#File judgment
#Method 1
for entry in os.listdir(basepath):
if os.path.isfile(os.path.join(base_path, entry)):
print(entry)
#Method 2 python 3.5 or later
with os.scandir(basepath) as entries:
for entry in entries:
if entry.is_file():
print(entry.name)
#Method 3 python 3.4 or later
basepath = Path('my_directory')
for entry in basepath.iterdir():
if entry.is_file():
print(entry.name)
# or
basepath = Path('my_directory')
files_in_basepath = (entry for entry in basepath.iterdir() if entry.is_file())
for item in files_in_basepath:
print(item.name)
#Get subfolder
#Method 1
for entry in os.listdir(basepath):
if os.path.isdir(os.path.join(basepath, entry)):
print(entry)
#Method 2
with os.scandir(basepath) as entries:
for entry in entries:
if entry.is_dir():
print(entry.name)
#Method 3
for entry in basepath.iterdir():
if entry.is_dir():
print(entry.name)
#Get file edit time
#Method 1
with os.scandir('my_directory') as entries:
for entry in entries:
info = entry.stat()
print(info.st_mtime)
#Method 2
for entry in basepath.iterdir():
info = entry.stat()
print(info.st_mtime)
#* Time conversion related
timestamp = int(info.st_mtime)#Delete ms seconds
dt = datetime.datetime.utcfromtimestamp(info.st_mtime) #st_mtime → datetime
dt = dt.strftime('%Y-%m-%d %H:%M:%S')
#https://strftime.org/
#https://docs.python.org/3/library/datetime.html#strftime-and-strptime-behavior
#Create folder
#Method 1
try:
os.mkdir('directory')
except FileExistsError as e:
print(e)
#Method 2
p = Path('directory')
try:
p.mkdir()
except FileExistsError as e:
print(e)
#* When ignoring the error,
p.mkdir(exist_ok=True)
#* Create subfolder
os.makedirs('floder/subf/sub')
#Creating a temporary file
from tempfile import TemporaryFile
#Temporary file creation & data entry
fp = TemporaryFile('w+t')
fp.write('Hello World!')
#Data read
fp.seek(0)
data = fp.read()
print(data)
#Close file (automatic deletion)
fp.close()
#with version
with TemporaryFile('w+t') as fp:
fp.write('i am good man!')
fp.seek(0)
fp.read()
#Creating a temporary folder
import tempfile
with tempfile.TemporaryDirectory() as tmpdir:
print('Created temporary directory ', tmpdir)
print(os.path.exists(tmpdir))
#File deletion
os.remove(file)
#or
os.unlink(file)
#Delete folder
os.rmdir(dir)
#or
dir = Path('my_documents/bad_dir')
dir.rmdir()
#Delete folder tree
shutil.rmtree(dir_tree)
#File copy
shutil.copy(src, dst)
#or
shutil.copy2(src, dst)#Copy file properties
#Copy of folder tree
dst = shutil.copytree('data', 'databackup')
#Move files and folders
dst = shutil.move('dir/', 'backup/')
#Rename files and folders
os.rename('first.zip', 'first_01.zip')
#.Find txt file
#Strings and Character Data in Python
#https://realpython.com/python-strings/
#Method 1
for f_name in os.listdir('directory'):
if f_name.endswith('.txt'):
print(f_name)
#Method 2
import fnmatch
for f_name in os.listdir('directory'):
if fnmatch.fnmatch(f_name, 'data_*_2020*.txt'):
print(f_name)
#Method 3
import glob
for name in glob.glob('*/*/*[0-9]*.txt'):#Subfolder, containing characters 0-9.Find the txt file
print(name)
#Read zip file
import zipfile
with zipfile.ZipFile('data.zip', 'r') as zipobj:
for names in zipobj.namelist():
if os.path.isfile(names)
info = zipobj.getinfo(names)
print(info.file_size,bar_info.date_time,bar_info.filename)
#Unzip the specified file from the zip file
data_zip = zipfile.ZipFile('data.zip', 'r')
#data.file1 in the zip.Unzip py to working directory
data_zip.extract('file1.py')
#Unzip everything to the specified folder
data_zip.extractall(path='extract_dir/')
#If you have a password
data_zip.extractall(path='extract_dir', pwd='password')
data_zip.close()
#Create & add zip file
#Create
file_list = ['file1.py', 'sub_dir/', 'sub_dir/bar.py', 'sub_dir/foo.py']
with zipfile.ZipFile('new.zip', 'w') as new_zip:
for name in file_list:
new_zip.write(name)
#add to
with zipfile.ZipFile('new.zip', 'a') as new_zip:
new_zip.write('data.txt')
new_zip.write('latin.txt')
#Read tar file
import tarfile
with tarfile.open('example.tar', 'r') as tar_file:
#mode :['r','r:gz','r:bz2','w','w:gz','w:xz','a']
for entry in tar_file.getmembers():
print(entry.name)
print(' Modified:', time.ctime(entry.mtime))
print(' Size :', entry.size, 'bytes')
#or
f = tar_file.extractfile('app.py')
f.read()
#Decompress the specified file from the tar file
tar = tarfile.open('example.tar', mode='r')
#Unzip to current directory
tar.extract('README.md')
#Unzip everything to the specified folder
tar.extractall(path="extracted/")
#Create & add tar file
#Create
import tarfile
file_list = ['app.py', 'config.py', 'CONTRIBUTORS.md', 'tests.py']
with tarfile.open('packages.tar', mode='w') as tar:
for file in file_list:
tar.add(file)
#add to
with tarfile.open('package.tar', mode='a') as tar:
tar.add('foo.bar')
#Verification
with tarfile.open('package.tar', mode='r') as tar:
for member in tar.getmembers():
print(member.name)
#shutil.make_archive()Folder compression with
import shutil
#backup the files in the data folder.Compress to tar
shutil.make_archive('data/backup', 'tar', 'data/')
#Defrost
shutil.unpack_archive('backup.tar', 'extract_dir/')
#Reading multiple files
#https://docs.python.org/3/library/fileinput.html
import fileinput,sys
for line in fileinput.input():
if fileinput.isfirstline():
print(f'\n--- Reading {fileinput.filename()} ---')
print(' -> ' + line, end='')
IT memos for non-IT industries
Recommended Posts