Pharmaceutical company researchers summarized file scanning in Python

Introduction

Here, we will introduce how to use the os module and pathlib module as a file scanning method using Python.

os module

The os module is a basic module for handling files and directories. The methods that are often used are as follows.

import os


directory = 'Directory name'
file = 'file name'

file_path = os.path.join(directory, file)
print(file_path) #Directory name/file name
print(os.path.isfile(file_path)) # True
print(os.path.isdir(file_path)) # False
print(os.path.isdir(directory)) # True
print(os.path.exists(file_path)) # True

entry_list = []
for entry in os.listdir(directory):
    entry_list.append(entry)
print(directory_list)

directory_list = []
file_list = []
path_list = []
for root, dirs, files in os.walk(directory):
    for drc in dirs:
        directory_list.append(drc)
    for file in files:
        file_list.append(file)
        file_path.append(os.path.join(root, file))
print(directory_list)
print(file_list)
print(path_list)

pathlib module

Since Python 3.4, you can use the pathlib module.

from pathlib import Path


p_dir = Path('Directory name')
p_file = Path('file name')

p_path = p_dir / p_file
p_path.mkdir(parents=True, exist_ok=True)
print(p_path)

print(p_path.parts)
print(p_path.parent)
print(p_path.name)
print(p_path.stem)
print(p_path.suffix)

print(p_path.is_file()) # True
print(p_path.is_dir()) # False
print(p_dir.is_dir()) # True
print(p_path.exists()) # True

p_path.rmdir()
print(p_path.exists()) # False

Summary

Here, I explained about the os module and the pathlib module. There is still a lack of explanation, so I will add it later.

Reference materials / links

What is the programming language Python? Can it be used for AI and machine learning?

Recommended Posts

Pharmaceutical company researchers summarized file scanning in Python
Pharmaceutical company researchers summarized classes in Python
Pharmaceutical company researchers summarized functions in Python
Pharmaceutical company researchers summarized variables in Python
Pharmaceutical company researchers summarized regular expressions in Python
Pharmaceutical company researchers summarized Python control statements
Pharmaceutical company researchers summarized Python unit tests
Pharmaceutical company researchers summarized Python exception handling
Pharmaceutical company researchers have summarized the operators used in Python
Pharmaceutical company researchers summarized Python coding standards
Pharmaceutical company researchers summarized database operations using Python
Pharmaceutical company researchers summarized SciPy
Pharmaceutical company researchers summarized RDKit
Pharmaceutical company researchers summarized scikit-learn
Pharmaceutical company researchers summarized Pandas
Pharmaceutical company researchers summarized NumPy
Pharmaceutical company researchers summarized Matplotlib
Pharmaceutical company researchers summarized Seaborn
Pharmaceutical company researchers summarized Python's comprehensions
Pharmaceutical company researchers summarized Python's data structures
File processing in Python
How to install Python for pharmaceutical company researchers
File operations in Python
File / folder path manipulation in Python
Save the binary file in Python
Create a binary file in Python
ORC, Parquet file operations in Python
A pharmaceutical company researcher summarized the basic description rules of Python
Exclusive control with lock file in Python
Convert psd file to png in Python
Read the file line by line in Python
Read the file line by line in Python
Exclusive file access between processes in Python
[GPS] Create a kml file in Python
[Python] Read the specified line in the file
Create a GIF file using Pillow in Python
File DL, byte value and delete in Python3
Read a file containing garbled lines in Python
From file to graph drawing in Python. Elementary elementary
How to create a JSON file in Python
Read table data in PDF file with Python
[Python] Implemented automation in excel file copying work
Transpose CSV file in Python Part 2: Performance measurement
GUI (WxPython) executable file (pyInstaller) [Windows] in Python3
Create a MIDI file in Python using pretty_midi
Standard .py file used in Python trials (template)-2020
Sample to put Python Kivy in one file
Speed evaluation of CSV file output in Python
Quadtree in Python --2
Python in optimization
CURL in python
Script python file
Geocoding in python
SendKeys in Python
Meta-analysis in Python
Unittest in python
Epoch in Python
Discord in Python
Sudoku in Python
DCI in Python
Python file processing