1. ã¯ããã«
Pythonã§ãã©ã«ãå ã®ãã¡ã€ã«åãååŸããæ¹æ³ã¯ãããã°ã©ãã³ã°åå¿è ããäžçŽè ã«ãšã£ãŠéåžžã«æçšãªã¹ãã«ã§ãããã¡ã€ã«åãååŸããããšã§ã倧éã®ããŒã¿åŠçããã¡ã€ã«æäœãå¹çåã§ãããããæ¥åã®èªååãããŒã¿æŽçã«åœ¹ç«ã¡ãŸãã
ãã®èšäºã§ã¯ãPythonã䜿ã£ããã©ã«ãå ã®ãã¡ã€ã«åååŸæ¹æ³ã段éçã«è§£èª¬ããŸããåããŠåŠã¶æ¹ã§ãå®è·µã§ãããããã³ãŒãäŸã亀ããŠãããããã説æããŸãã
2. osã¢ãžã¥ãŒã«ã䜿ã£ãåºæ¬çãªãã¡ã€ã«åååŸæ¹æ³
Pythonæšæºã©ã€ãã©ãªã®os
ã¢ãžã¥ãŒã«ã䜿çšããã°ããã©ã«ãå
ã®ãã¡ã€ã«åãç°¡åã«ååŸã§ããŸãããã®æ¹æ³ã¯ãPythonãåããŠäœ¿ãæ¹ã«ãé©ããŠãããã·ã³ãã«ã§çŽæçãªã³ãŒãæ§é ãç¹åŸŽã§ãã
os.listdir()é¢æ°ã®åºæ¬çãªäœ¿ãæ¹
os.listdir()
ã¯ãæå®ãããã£ã¬ã¯ããªå
ã®ãã¹ãŠã®é
ç®ïŒãã¡ã€ã«ããã©ã«ãïŒã®ååããªã¹ããšããŠè¿ããŸãã
import os
# ãã©ã«ãå
ã®ãã¡ã€ã«ãšãã£ã¬ã¯ããªãååŸ
folder_path = "sample_folder"
items = os.listdir(folder_path)
print("ãã©ã«ãå
ã®é
ç®:")
print(items)
äžèšã®ã³ãŒããå®è¡ãããšãæå®ãããã©ã«ãå ã®ãã¡ã€ã«åãšãã£ã¬ã¯ããªåããªã¹ã圢åŒã§è¡šç€ºãããŸãã
ãã¡ã€ã«åã®ã¿ãæœåºããæ¹æ³
ååŸãããªã¹ãã«ã¯ããã£ã¬ã¯ããªåãå«ãŸããããšããããŸããos.path.isfile()
ã䜿ãããšã§ããã¡ã€ã«ã®ã¿ãæœåºããããšãå¯èœã§ãã
import os
# ãã©ã«ãå
ã®ãã¡ã€ã«åã®ã¿ãååŸ
folder_path = "sample_folder"
files = [f for f in os.listdir(folder_path) if os.path.isfile(os.path.join(folder_path, f))]
print("ãã©ã«ãå
ã®ãã¡ã€ã«:")
print(files)
ãã®ã³ãŒãã§ã¯ãåé ç®ããã¡ã€ã«ã§ãããããã§ãã¯ããŠããã¡ã€ã«åã®ã¿ããªã¹ãã«æ ŒçŽããŠããŸãã
䜿çšäŸ
äŸãã°ããããã©ã«ãå ã«ä¿åãããç»åãã¡ã€ã«ã®ãªã¹ããäœæãããå Žåã以äžã®ããã«æŽ»çšã§ããŸãã
import os
# ç»åãã¡ã€ã«ãååŸ
folder_path = "images"
image_files = [f for f in os.listdir(folder_path) if f.endswith((".png", ".jpg", ".jpeg"))]
print("ç»åãã¡ã€ã«äžèŠ§:")
print(image_files)
ãã®æ¹æ³ã®å©ç¹
- ã·ã³ãã«ã§åãããããã
- Pythonæšæºã©ã€ãã©ãªã ãã§å®è£ å¯èœã
- å°èŠæš¡ãªãã©ã«ãå ã®ãã¡ã€ã«æäœã«æé©ã
泚æç¹
- é ããã¡ã€ã«ãç¹æ®ãªãã¡ã€ã«åãå«ãŸããå Žåããããããå¿ èŠã«å¿ããŠãã£ã«ã¿ãªã³ã°ãè¡ãå¿ èŠããããŸãã
- 倧èŠæš¡ãªãã£ã¬ã¯ããªã§äœ¿çšãããšããã©ãŒãã³ã¹ã«åœ±é¿ãåãŒãå¯èœæ§ããããŸãã
3. ã¯ã€ã«ãã«ãŒãæ€çŽ¢ãå¯èœãªglobã¢ãžã¥ãŒã«ã®æŽ»çš
Pythonã®glob
ã¢ãžã¥ãŒã«ã䜿ãã°ãç¹å®ã®ãã¿ãŒã³ã«äžèŽãããã¡ã€ã«åãå¹ççã«ååŸã§ããŸããos
ã¢ãžã¥ãŒã«ãšç°ãªããã¯ã€ã«ãã«ãŒããå©çšããŠãã¡ã€ã«åã®æ€çŽ¢æ¡ä»¶ãæè»ã«èšå®ã§ããã®ãç¹åŸŽã§ãã
globã¢ãžã¥ãŒã«ã®åºæ¬çãªäœ¿ãæ¹
glob.glob()
ã䜿ãããšã§ãæå®ãããã¿ãŒã³ã«äžèŽãããã¡ã€ã«ããã©ã«ããååŸã§ããŸãã
import glob
# ãã©ã«ãå
ã®ãã¹ãŠã®ãã¡ã€ã«ãååŸ
folder_path = "sample_folder"
files = glob.glob(f"{folder_path}/*")
print("ãã©ã«ãå
ã®ãã¡ã€ã«:")
print(files)
ãã®ã³ãŒãã§ã¯ãæå®ãããã©ã«ãå ã®ãã¹ãŠã®é ç®ïŒãã¡ã€ã«ããã©ã«ãïŒã®ãã¹ãååŸãããŸãã
ç¹å®ã®ãã¿ãŒã³ã«äžèŽãããã¡ã€ã«ãååŸ
äŸãã°ãç¹å®ã®æ¡åŒµåãæã€ãã¡ã€ã«ã ããååŸããå Žåã¯ä»¥äžã®ããã«èšè¿°ããŸãã
import glob
# æå®ããæ¡åŒµåã®ãã¡ã€ã«ãååŸ
folder_path = "sample_folder"
text_files = glob.glob(f"{folder_path}/*.txt")
print("ããã¹ããã¡ã€ã«äžèŠ§:")
print(text_files)
ãã®äŸã§ã¯ã.txt
æ¡åŒµåãæã€ãã¡ã€ã«ã ããååŸãããŸããåæ§ã«ãè€æ°ã®æ¡åŒµåãæå®ãããå Žåã¯ä»¥äžã®ããã«èšè¿°ããŸãã
import glob
# è€æ°ã®æ¡åŒµåãæå®ããŠãã¡ã€ã«ãååŸ
folder_path = "sample_folder"
files = glob.glob(f"{folder_path}/*.txt") + glob.glob(f"{folder_path}/*.csv")
print("ããã¹ããšCSVãã¡ã€ã«äžèŠ§:")
print(files)
ååž°çã«ãã¡ã€ã«ãååŸ
glob
ã¢ãžã¥ãŒã«ã®recursive=True
ãªãã·ã§ã³ã䜿çšããããšã§ããµããã£ã¬ã¯ããªå
ã®ãã¡ã€ã«ãå«ããŠååŸã§ããŸãã
import glob
# ãµããã£ã¬ã¯ããªå
ã®ãã¹ãŠã®ãã¡ã€ã«ãååž°çã«ååŸ
folder_path = "sample_folder"
files = glob.glob(f"{folder_path}/**/*", recursive=True)
print("ãµããã£ã¬ã¯ããªå
ã®ãã¹ãŠã®ãã¡ã€ã«:")
print(files)
ã¯ã€ã«ãã«ãŒãã®äœ¿çšäŸ
*
ïŒä»»æã®æååãè¡šãã?
ïŒä»»æã®1æåãè¡šãã[abc]
ïŒæ¬åŒ§å ã®ä»»æã®æåãè¡šãã
import glob
# ãã¡ã€ã«åã"data"ã§å§ãŸãä»»æã®ãã¡ã€ã«ãååŸ
folder_path = "sample_folder"
files = glob.glob(f"{folder_path}/data*")
print("dataã§å§ãŸããã¡ã€ã«äžèŠ§:")
print(files)
å©ç¹ãšçšé
- æè»ãªãã¿ãŒã³æå®ãå¯èœã§ããã¡ã€ã«åã®æ€çŽ¢ã«ç¹åããŠããã
os
ã¢ãžã¥ãŒã«ãšæ¯èŒããŠãã³ãŒããç°¡æœã«ãªãã- ååž°æ€çŽ¢ãç°¡åã«è¡ããã
泚æç¹
- ãã¿ãŒã³ã«äžèŽããªãå Žåã¯ç©ºã®ãªã¹ããè¿ããããããçµæã®ç¢ºèªãå¿ èŠã§ãã
- ååž°æ€çŽ¢ã§èšå€§ãªæ°ã®ãã¡ã€ã«ãååŸãããå¯èœæ§ããããããé©åã«ãã£ã«ã¿ãªã³ã°ããããšãæšå¥šããŸãã
4. ãªããžã§ã¯ãæåçã«æäœããpathlibã¢ãžã¥ãŒã«
Pythonã®pathlib
ã¢ãžã¥ãŒã«ã¯ããã¡ã€ã«ãã¹ã®æäœããªããžã§ã¯ãæåã§æ±ããããã«ãããã¢ãã³ã§çŽæçãªæ¹æ³ã§ããããã«ãããã³ãŒãã®å¯èªæ§ãåäžããæäœãããã·ã³ãã«ã«ãªããŸãã
pathlibã¢ãžã¥ãŒã«ã®åºæ¬çãªäœ¿ãæ¹
pathlib
ã¢ãžã¥ãŒã«ã®Path
ãªããžã§ã¯ãã䜿çšãããšããã£ã¬ã¯ããªå
ã®é
ç®ãç°¡åã«ååŸã§ããŸãã
from pathlib import Path
# ãã©ã«ãå
ã®é
ç®ãååŸ
folder_path = Path("sample_folder")
items = list(folder_path.iterdir())
print("ãã©ã«ãå
ã®é
ç®:")
print(items)
ãã®ã³ãŒãã§ã¯ããã©ã«ãå
ã®ãã¹ãŠã®é
ç®ïŒãã¡ã€ã«ããã©ã«ãïŒãPath
ãªããžã§ã¯ãã®ãªã¹ããšããŠè¿ãããŸãã
ãã¡ã€ã«åã®ã¿ãæœåº
ãã©ã«ãå
ã®é
ç®ãããã¡ã€ã«ã ããååŸããã«ã¯ãis_file()
ã¡ãœããã䜿çšããŸãã
from pathlib import Path
# ãã©ã«ãå
ã®ãã¡ã€ã«ãååŸ
folder_path = Path("sample_folder")
files = [f for f in folder_path.iterdir() if f.is_file()]
print("ãã©ã«ãå
ã®ãã¡ã€ã«:")
print(files)
ãã®ã³ãŒãã§ã¯ããã£ã¬ã¯ããªã¯é€å€ããããã¡ã€ã«ã®ã¿ããªã¹ãã«æ ŒçŽãããŸãã
æ¡åŒµåã§ãã£ã«ã¿ãªã³ã°
ç¹å®ã®æ¡åŒµåãæã€ãã¡ã€ã«ã ããååŸããã«ã¯ãæ¡åŒµåãæ¡ä»¶ãšããŠæå®ããŸãã
from pathlib import Path
# ãã©ã«ãå
ã®ç¹å®æ¡åŒµåãæã€ãã¡ã€ã«ãååŸ
folder_path = Path("sample_folder")
text_files = [f for f in folder_path.iterdir() if f.is_file() and f.suffix == ".txt"]
print("ããã¹ããã¡ã€ã«:")
print(text_files)
suffix
å±æ§ã䜿ãããšã§ããã¡ã€ã«ã®æ¡åŒµåãç°¡åã«ååŸã§ããŸãã
ååž°çã«ãã¡ã€ã«ãååŸ
ãµããã£ã¬ã¯ããªå
ã®ãã¡ã€ã«ãå«ããŠååŸããå Žåã¯ãrglob()
ã¡ãœããã䜿çšããŸãã
from pathlib import Path
# ãµããã£ã¬ã¯ããªå
ã®ãã¹ãŠã®ãã¡ã€ã«ãååŸ
folder_path = Path("sample_folder")
files = list(folder_path.rglob("*"))
print("ãµããã£ã¬ã¯ããªå
ã®ãã¹ãŠã®ãã¡ã€ã«:")
print(files)
ããã«ãç¹å®ã®ãã¿ãŒã³ã«äžèŽãããã¡ã€ã«ãååž°çã«æ€çŽ¢ããããšãå¯èœã§ãã
from pathlib import Path
# ãµããã£ã¬ã¯ããªå
ã®ç¹å®æ¡åŒµåãæã€ãã¡ã€ã«ãååŸ
folder_path = Path("sample_folder")
text_files = list(folder_path.rglob("*.txt"))
print("ååž°çã«ååŸããããã¹ããã¡ã€ã«:")
print(text_files)
å©ç¹ãšçšé
- ãªããžã§ã¯ãæåã§çŽæçãªã³ãŒããæžããã
- ååž°çãªæäœããã£ã«ã¿ãªã³ã°ãç°¡åã«å®çŸã§ããã
- Python 3.4以éã§äœ¿çšå¯èœã§ãææ°ã®Pythonã§ã¯æšå¥šãããã¢ãžã¥ãŒã«ã
泚æç¹
- ãµããŒããããŠããªãPythonã®ããŒãžã§ã³ïŒ3.4æªæºïŒã§ã¯äœ¿çšã§ããŸããã
- 倧èŠæš¡ãªãã£ã¬ã¯ããªæäœã§ã¯ããã©ãŒãã³ã¹ã«æ³šæãå¿ èŠã§ãã
5. ãµããã£ã¬ã¯ããªå ã®ãã¡ã€ã«ãååž°çã«ååŸããæ¹æ³
ãã©ã«ãå
ã®ãã¡ã€ã«ãååŸããéããµããã£ã¬ã¯ããªã«ãããã¡ã€ã«ãå«ããŠååž°çã«ååŸãããå ŽåããããŸãããã®ã»ã¯ã·ã§ã³ã§ã¯ãos
ã¢ãžã¥ãŒã«ãšãã®ä»ã®æ¹æ³ã䜿ã£ãŠãååž°çã«ãã¡ã€ã«ãååŸããææ³ã解説ããŸãã
os.walk()ã䜿ã£ãæ¹æ³
os.walk()
ã¯ãæå®ãããã£ã¬ã¯ããªãšãã®ãµããã£ã¬ã¯ããªå
ãååž°çã«æ¢çŽ¢ãããã©ã«ãåããµããã©ã«ãåããã¡ã€ã«åãååŸã§ããŸãã
import os
# ãµããã£ã¬ã¯ããªå
ã®ãã¹ãŠã®ãã¡ã€ã«ãååŸ
folder_path = "sample_folder"
for root, dirs, files in os.walk(folder_path):
for file in files:
print(f"ãã¡ã€ã«: {os.path.join(root, file)}")
ã³ãŒãã®èª¬æïŒ
os.walk()
ã¯ã(ãã©ã«ããã¹, ãµããã©ã«ããªã¹ã, ãã¡ã€ã«ãªã¹ã)ãã¿ãã«ãšããŠè¿ããŸããos.path.join()
ã䜿ã£ãŠããã¡ã€ã«ã®ãã«ãã¹ãäœæããŠããŸãã
ãã¡ã€ã«ãªã¹ãããŸãšããŠååŸ
ååŸãããã¹ãŠã®ãã¡ã€ã«ããªã¹ãã«æ ŒçŽãããå Žåã¯ã以äžã®ããã«æžããŸãã
import os
# ãµããã£ã¬ã¯ããªå
ã®ãã¹ãŠã®ãã¡ã€ã«ããªã¹ãã«æ ŒçŽ
folder_path = "sample_folder"
all_files = []
for root, dirs, files in os.walk(folder_path):
for file in files:
all_files.append(os.path.join(root, file))
print("ãµããã£ã¬ã¯ããªå
ã®ãã¹ãŠã®ãã¡ã€ã«:")
print(all_files)
ç¹å®ã®æ¡åŒµåãæã€ãã¡ã€ã«ã®ã¿ååŸ
äŸãã°ã.txt
ãã¡ã€ã«ã ããååŸãããå Žåã次ã®ããã«æ¡ä»¶ãè¿œå ããŸãã
import os
# ãµããã£ã¬ã¯ããªå
ã®ç¹å®ã®æ¡åŒµåãæã€ãã¡ã€ã«ãååŸ
folder_path = "sample_folder"
txt_files = []
for root, dirs, files in os.walk(folder_path):
for file in files:
if file.endswith(".txt"):
txt_files.append(os.path.join(root, file))
print("ããã¹ããã¡ã€ã«:")
print(txt_files)
globã¢ãžã¥ãŒã«ã䜿ã£ãååž°çãªååŸ
glob
ã¢ãžã¥ãŒã«ã®**
ã䜿ãã°ãç°¡åã«ååž°çãªæ€çŽ¢ãå¯èœã§ãã
import glob
# ååž°çã«ãã¹ãŠã®ãã¡ã€ã«ãååŸ
folder_path = "sample_folder"
all_files = glob.glob(f"{folder_path}/**/*", recursive=True)
print("ååž°çã«ååŸãããã¡ã€ã«:")
print(all_files)
ãŸããç¹å®ã®æ¡åŒµåãæã€ãã¡ã€ã«ã®ã¿ãååŸãããå Žåãç°¡åã§ãã
import glob
# ååž°çã«ç¹å®ã®æ¡åŒµåãæã€ãã¡ã€ã«ãååŸ
folder_path = "sample_folder"
txt_files = glob.glob(f"{folder_path}/**/*.txt", recursive=True)
print("ååž°çã«ååŸããããã¹ããã¡ã€ã«:")
print(txt_files)
pathlibã¢ãžã¥ãŒã«ã䜿ã£ãååž°çãªååŸ
pathlib
ã¢ãžã¥ãŒã«ã®rglob()
ã¡ãœããã¯ãååž°çãªãã¡ã€ã«ååŸãçŽæçã«å®çŸã§ããŸãã
from pathlib import Path
# ååž°çã«ãã¹ãŠã®ãã¡ã€ã«ãååŸ
folder_path = Path("sample_folder")
all_files = list(folder_path.rglob("*"))
print("ååž°çã«ååŸãããã¡ã€ã«:")
print(all_files)
ç¹å®ã®æ¡åŒµåãæå®ããå Žåãç°¡åã§ãã
from pathlib import Path
# ååž°çã«ç¹å®ã®æ¡åŒµåãæã€ãã¡ã€ã«ãååŸ
folder_path = Path("sample_folder")
txt_files = list(folder_path.rglob("*.txt"))
print("ååž°çã«ååŸããããã¹ããã¡ã€ã«:")
print(txt_files)
åæ¹æ³ã®æ¯èŒ
æ¹æ³ | ç¹åŸŽ | å©ç¹ | 泚æç¹ |
---|---|---|---|
os.walk() | ååž°çãªãã¡ã€ã«æ¢çŽ¢ãæåã§å®è£ | ã«ã¹ã¿ãã€ãºæ§ãé«ã | ã³ãŒããé·ããªãããšããã |
glob | ã¯ã€ã«ãã«ãŒãã䜿ã£ãç°¡æœãªèšè¿°ãå¯èœ | ååž°çãªæ€çŽ¢ãçãã³ãŒãã§å®çŸå¯èœ | ã¯ã€ã«ãã«ãŒããã¿ãŒã³ã®èšèšãå¿ èŠ |
pathlib.rglob() | ãªããžã§ã¯ãæåçã§Pythonicãªæžãæ¹ãå¯èœ | å¯èªæ§ãé«ããææ°ã®Pythonã³ãŒãã«é©å | Python 3.4以éã§ã®ã¿å©çšå¯èœ |
ã
6. ç¹å®æ¡ä»¶ã§ã®ãã¡ã€ã«åãã£ã«ã¿ãªã³ã°ãšå¿çšäŸ
ç¹å®ã®æ¡ä»¶ã§ãã¡ã€ã«ããã£ã«ã¿ãªã³ã°ããããšã¯ãååŸãããã¡ã€ã«åãªã¹ããæŽçããç®çã«å¿ããããŒã¿æäœãè¡ãéã«åœ¹ç«ã¡ãŸãããã®ã»ã¯ã·ã§ã³ã§ã¯ãPythonã䜿ã£ãŠæ¡åŒµåããã¡ã€ã«åãããã«ã¯æ£èŠè¡šçŸã掻çšããŠãã¡ã€ã«ãçµã蟌ãæ¹æ³ã解説ããŸãã
æ¡åŒµåã§ã®ãã£ã«ã¿ãªã³ã°
æå®ããæ¡åŒµåã®ãã¡ã€ã«ã®ã¿ãååŸãããå Žåããªã¹ãå å è¡šèšãæ¡ä»¶åå²ãå©çšããŸãã
äŸ: osã¢ãžã¥ãŒã«ã䜿çš
import os
# æå®ããæ¡åŒµåã®ãã¡ã€ã«ãååŸ
folder_path = "sample_folder"
txt_files = [f for f in os.listdir(folder_path) if f.endswith(".txt")]
print("ããã¹ããã¡ã€ã«:")
print(txt_files)
äŸ: pathlibã¢ãžã¥ãŒã«ã䜿çš
pathlib
ã¢ãžã¥ãŒã«ã䜿çšãããšãæ¡åŒµåã®ãã£ã«ã¿ãªã³ã°ãããã«ç°¡æœã«èšè¿°ã§ããŸãã
from pathlib import Path
# æ¡åŒµåã.txtã®ãã¡ã€ã«ãååŸ
folder_path = Path("sample_folder")
txt_files = [f for f in folder_path.iterdir() if f.suffix == ".txt"]
print("ããã¹ããã¡ã€ã«:")
print(txt_files)
ãã¡ã€ã«åã«ç¹å®ã®æååãå«ãå Žå
ç¹å®ã®æååããã¡ã€ã«åã«å«ãŸããŠããããå€å®ããã«ã¯ãin
æŒç®åã䜿ããŸãã
import os
# ãã¡ã€ã«åã«"report"ãå«ããã¡ã€ã«ãååŸ
folder_path = "sample_folder"
report_files = [f for f in os.listdir(folder_path) if "report" in f]
print("reportãå«ããã¡ã€ã«:")
print(report_files)
æ£èŠè¡šçŸã䜿ã£ãé«åºŠãªãã£ã«ã¿ãªã³ã°
æ£èŠè¡šçŸã䜿ãã°ãããæè»ãªæ¡ä»¶ã§ãã¡ã€ã«ããã£ã«ã¿ãªã³ã°ã§ããŸããäŸãã°ããã¡ã€ã«åã®ç¹å®ã®ãã¿ãŒã³ãæ¢ãå Žåã«æçšã§ãã
import os
import re
# ãã¡ã€ã«åãæ°åã®ã¿ã§æ§æããããã¡ã€ã«ãååŸ
folder_path = "sample_folder"
pattern = re.compile(r"^\d+$")
files = [f for f in os.listdir(folder_path) if pattern.match(f)]
print("æ°åã®ã¿ã®ãã¡ã€ã«å:")
print(files)
ãã¡ã€ã«ãµã€ãºã§ãã£ã«ã¿ãªã³ã°
ãã¡ã€ã«ã®ãµã€ãºãæ¡ä»¶ã«ãã£ã«ã¿ãªã³ã°ãããå Žåã¯ãos.path.getsize()
ãå©çšããŸãã
import os
# ãµã€ãºã1MB以äžã®ãã¡ã€ã«ãååŸ
folder_path = "sample_folder"
large_files = [f for f in os.listdir(folder_path) if os.path.getsize(os.path.join(folder_path, f)) > 1 * 1024 * 1024]
print("1MB以äžã®ãã¡ã€ã«:")
print(large_files)
å¿çšäŸ
1. æ¡åŒµåã§åé¡ãã
è€æ°ã®æ¡åŒµåãæã€ãã¡ã€ã«ãåé¡ãããå Žåã以äžã®ããã«æžããŸãã
import os
# ãã¡ã€ã«ãæ¡åŒµåã§åé¡
folder_path = "sample_folder"
files_by_extension = {}
for f in os.listdir(folder_path):
ext = os.path.splitext(f)[1] # æ¡åŒµåãååŸ
if ext not in files_by_extension:
files_by_extension[ext] = []
files_by_extension[ext].append(f)
print("æ¡åŒµåããšã®åé¡:")
print(files_by_extension)
2. æ¥ä»ã§çµã蟌ã
ãã¡ã€ã«ã®æŽæ°æ¥æãåºæºã«ãã£ã«ã¿ãªã³ã°ããã«ã¯ãos.path.getmtime()
ã䜿çšããŸãã
import os
import time
# äžé±é以å
ã«æŽæ°ããããã¡ã€ã«ãååŸ
folder_path = "sample_folder"
one_week_ago = time.time() - 7 * 24 * 60 * 60
recent_files = [f for f in os.listdir(folder_path) if os.path.getmtime(os.path.join(folder_path, f)) > one_week_ago]
print("æè¿æŽæ°ããããã¡ã€ã«:")
print(recent_files)
åæ¹æ³ã®çšéãšå©äŸ¿æ§
ãã£ã«ã¿ãªã³ã°æ¡ä»¶ | 䜿çšæ¹æ³ | çšé |
---|---|---|
æ¡åŒµå | f.endswith(".txt") | ãã¡ã€ã«ã®çš®é¡ã§åé¡ãã |
ç¹å®æåå | "keyword" in f | ç¹å®ã®çšéã«é¢é£ãããã¡ã€ã«ãæ¢ã |
æ£èŠè¡šçŸ | re.match(pattern, f) | è€éãªååãã¿ãŒã³ã®ãã¡ã€ã«ãæ€çŽ¢ |
ãã¡ã€ã«ãµã€ãº | os.path.getsize() | 倧容éãã¡ã€ã«ã®æ€åº |
æŽæ°æ¥æ | os.path.getmtime() | æè¿äœ¿çšããããã¡ã€ã«ãèŠã€ãã |
ã
7. å®è·µäŸïŒååŸãããã¡ã€ã«åãªã¹ãã®äœ¿ãæ¹
ååŸãããã¡ã€ã«åãªã¹ããã©ã®ããã«æŽ»çšãããã¯ãçšéãç®çã«å¿ããŠå€å²ã«ããããŸãããã®ã»ã¯ã·ã§ã³ã§ã¯ãäžè¬çãªæŽ»çšã·ããªãªãšå ·äœçãªã³ãŒãäŸãããã€ã玹ä»ããŸãã
ãã¡ã€ã«å 容ã®äžæ¬åŠç
ååŸãããã¡ã€ã«ãªã¹ããçšããŠãåãã¡ã€ã«ã®å 容ãèªã¿èŸŒãã§åŠçããäŸã瀺ããŸãã
äŸïŒããã¹ããã¡ã€ã«ã®å 容ããŸãšããŠèªã¿èŸŒã
import os
# ãã©ã«ãå
ã®ããã¹ããã¡ã€ã«ãååŸ
folder_path = "sample_folder"
text_files = [f for f in os.listdir(folder_path) if f.endswith(".txt")]
# ãã¡ã€ã«å
容ããŸãšããŠèªã¿èŸŒã
all_content = ""
for file in text_files:
with open(os.path.join(folder_path, file), "r", encoding="utf-8") as f:
all_content += f.read() + "
"
print("ãã¹ãŠã®ããã¹ããã¡ã€ã«ã®å
容:")
print(all_content)
ãã¡ã€ã«ã®ãªããŒã
ååŸãããã¡ã€ã«åãªã¹ããå©çšããŠãäžæ¬ã§ãã¡ã€ã«ããªããŒã ããããšãå¯èœã§ãã
äŸïŒãã¡ã€ã«åã«ãã¬ãã£ãã¯ã¹ãè¿œå ãã
import os
# ãã©ã«ãå
ã®ãã¡ã€ã«ãååŸ
folder_path = "sample_folder"
files = os.listdir(folder_path)
# ãã¡ã€ã«åã«ãã¬ãã£ãã¯ã¹ãè¿œå
for file in files:
old_path = os.path.join(folder_path, file)
new_path = os.path.join(folder_path, f"new_{file}")
os.rename(old_path, new_path)
print("ãã¡ã€ã«åãå€æŽããŸããã")
ãã¡ã€ã«ãªã¹ãã®ä¿å
ååŸãããã¡ã€ã«åãªã¹ããå€éšãã¡ã€ã«ïŒããã¹ããã¡ã€ã«ãExcelïŒã«ä¿åããŠãåŸãã確èªã§ããããã«ããããšã䟿å©ã§ãã
äŸïŒãã¡ã€ã«åãªã¹ããããã¹ããã¡ã€ã«ã«ä¿åãã
import os
# ãã©ã«ãå
ã®ãã¡ã€ã«ãååŸ
folder_path = "sample_folder"
files = os.listdir(folder_path)
# ãã¡ã€ã«åãããã¹ããã¡ã€ã«ã«ä¿å
with open("file_list.txt", "w", encoding="utf-8") as f:
for file in files:
f.write(file + "
")
print("ãã¡ã€ã«åãªã¹ããä¿åããŸããã")
äŸïŒãã¡ã€ã«åãªã¹ããCSVã«ä¿åãã
import os
import csv
# ãã©ã«ãå
ã®ãã¡ã€ã«ãååŸ
folder_path = "sample_folder"
files = os.listdir(folder_path)
# ãã¡ã€ã«åãCSVã«ä¿å
with open("file_list.csv", "w", encoding="utf-8", newline="") as csvfile:
writer = csv.writer(csvfile)
writer.writerow(["ãã¡ã€ã«å"]) # ããããŒãè¿œå
for file in files:
writer.writerow([file])
print("ãã¡ã€ã«åãªã¹ããCSVã«ä¿åããŸããã")
ãã¡ã€ã«ã®ããã¯ã¢ããäœæ
ãã¡ã€ã«åãªã¹ããå©çšããŠãæå®ãããã©ã«ãã«ãã¡ã€ã«ã®ããã¯ã¢ãããäœæããäŸã§ãã
import os
import shutil
# ãã©ã«ãå
ã®ãã¡ã€ã«ãååŸ
source_folder = "sample_folder"
backup_folder = "backup_folder"
os.makedirs(backup_folder, exist_ok=True)
files = os.listdir(source_folder)
# ãã¡ã€ã«ãããã¯ã¢ãããã©ã«ãã«ã³ããŒ
for file in files:
shutil.copy(os.path.join(source_folder, file), os.path.join(backup_folder, file))
print("ããã¯ã¢ãããäœæããŸããã")
ç¹å®ã®ãã¡ã€ã«ã«åŠçãéå®ãã
ååŸãããã¡ã€ã«ãªã¹ãã䜿ã£ãŠãç¹å®ã®æ¡ä»¶ãæºãããã¡ã€ã«ã«å¯ŸããŠã®ã¿åŠçãå®è¡ããŸãã
äŸïŒç¹å®ã®æ¡åŒµåãæã€ãã¡ã€ã«ã®åé€
import os
# ãã©ã«ãå
ã®ç¹å®ã®æ¡åŒµåãæã€ãã¡ã€ã«ãåé€
folder_path = "sample_folder"
files = [f for f in os.listdir(folder_path) if f.endswith(".tmp")]
for file in files:
os.remove(os.path.join(folder_path, file))
print("äžèŠãªãã¡ã€ã«ãåé€ããŸããã")
å¿çšã·ããªãª
- ããŒã¿åéïŒå€§éã®CSVãã¡ã€ã«ãååŸããŠãããŒã¿ãçµ±åã
- ãã°ç®¡çïŒå€ããã°ãã¡ã€ã«ãç¹å®ããŠåé€ã
- ç»ååŠçïŒç¹å®ã®ãã©ã«ãå ã®ç»åã«å¯ŸããŠãªãµã€ãºãå€æãå®æœã
8. ãã©ãã«ã·ã¥ãŒãã£ã³ã°ïŒãšã©ãŒè§£æ¶ã®ãã³ã
ãã¡ã€ã«æäœããã©ã«ãå ã®ãã¡ã€ã«åååŸãè¡ãéã«ã¯ãããŸããŸãªãšã©ãŒãçºçããå¯èœæ§ããããŸãããã®ã»ã¯ã·ã§ã³ã§ã¯ããããããšã©ãŒãšãã®è§£æ±ºæ¹æ³ã解説ããŸãã
ãšã©ãŒ1: ãã©ã«ããèŠã€ãããªã
çºçç¶æ³
æå®ãããã©ã«ããååšããªãå ŽåãFileNotFoundError
ãçºçããŸãã
åå
- ãã©ã«ããã¹ãééã£ãŠããã
- ãã©ã«ããåé€ãããŠããã
察åŠæ¹æ³
os.path.exists()
ãpathlib.Path.exists()
ã§ãã©ã«ããååšããã確èªããŸãã
äŸ: ãã©ã«ããååšããã確èªããã³ãŒã
import os
folder_path = "sample_folder"
if not os.path.exists(folder_path):
print(f"ãšã©ãŒ: ãã©ã«ããèŠã€ãããŸãã ({folder_path})")
else:
print("ãã©ã«ããååšããŸãã")
ãšã©ãŒ2: ãã¡ã€ã«æäœæã®æš©éãšã©ãŒ
çºçç¶æ³
ãã¡ã€ã«ã®èªã¿åããæžã蟌ã¿ãè©Šã¿ãéã«ãPermissionError
ãçºçããŸãã
åå
- ãã¡ã€ã«ãŸãã¯ãã©ã«ãã«ã¢ã¯ã»ã¹æš©ããªãã
- ãã¡ã€ã«ãä»ã®ããã»ã¹ã«ãã£ãŠããã¯ãããŠããã
察åŠæ¹æ³
- ã¢ã¯ã»ã¹æš©éã確èªããŠä¿®æ£ããŸãã
- ãã¡ã€ã«ã䜿çšäžã§ãªãã確èªããŸãã
äŸ: ã¢ã¯ã»ã¹æš©ãšã©ãŒã®åŠç
import os
file_path = "sample_folder/sample_file.txt"
try:
with open(file_path, "r", encoding="utf-8") as f:
content = f.read()
print(content)
except PermissionError:
print(f"ãšã©ãŒ: ãã¡ã€ã«ã«ã¢ã¯ã»ã¹ããæš©éããããŸãã ({file_path})")
ãšã©ãŒ3: ãã¡ã€ã«ãã¹ãé·ããã
çºçç¶æ³
Windowsç°å¢ã§ã¯ããã¹ã260æåãè¶
ããå Žåã«ãšã©ãŒãçºçããããšããããŸãã
察åŠæ¹æ³
- Windowsã§ãé·ããã¹ãããµããŒãããèšå®ãæå¹åããŸãã
- ãã¡ã€ã«ããã©ã«ãåãççž®ããŸãã
äŸ: ãã¹ãççž®ããã³ãŒã
import os
# é·ããã¹ãççž®
long_path = "a/very/long/path/to/a/folder/with/a/long/file_name.txt"
short_path = os.path.basename(long_path)
print(f"ççž®ãã¹: {short_path}")
ãšã©ãŒ4: ç¹æ®æåãå«ããã¡ã€ã«åã®åŠç
çºçç¶æ³
ãã¡ã€ã«åã«ç¹æ®æåïŒäŸ: 空çœãç¹æ®èšå·ãéASCIIæåïŒãå«ãŸããå Žåããšã©ãŒãçºçããããšããããŸãã
察åŠæ¹æ³
- ãã¡ã€ã«åãæ£èŠåããŸãã
- éASCIIæåãåé€ãŸãã¯çœ®æããŸãã
äŸ: ãã¡ã€ã«åãæ£èŠåããã³ãŒã
import os
file_path = "sample_folder/ãµã³ãã« file!.txt"
normalized_file_path = file_path.replace(" ", "_").replace("!", "")
print(f"æ£èŠåããããã¡ã€ã«å: {normalized_file_path}")
ãšã©ãŒ5: ã¡ã¢ãªäžè¶³ãšã©ãŒ
çºçç¶æ³
éåžžã«å€§èŠæš¡ãªãã£ã¬ã¯ããªãåŠçããéãã¡ã¢ãªãäžè¶³ããŠãšã©ãŒãçºçããããšããããŸãã
察åŠæ¹æ³
- ãã¡ã€ã«ãäžæ¬åŠçããã®ã§ã¯ãªããå°éãã€åå²ããŠåŠçããŸãã
- ãã¡ã€ã«ãªã¹ããçæããéã«ãžã§ãã¬ãŒã¿ã䜿çšããŸãã
äŸ: ãžã§ãã¬ãŒã¿ã䜿çšããåŠç
import os
def get_files(folder_path):
for root, _, files in os.walk(folder_path):
for file in files:
yield os.path.join(root, file)
folder_path = "sample_folder"
for file in get_files(folder_path):
print(f"åŠçäž: {file}")
ãšã©ãŒ6: ãã¡ã€ã«ãããã¯ãããŠãã
çºçç¶æ³
ç¹å®ã®ã¢ããªã±ãŒã·ã§ã³ããã¡ã€ã«ãéããŠãããããåé€ãç·šéãã§ããªãå ŽåããããŸãã
察åŠæ¹æ³
- ãã¡ã€ã«ã䜿çšããŠããããã»ã¹ãç¹å®ããŠçµäºããŸãã
- ãã¡ã€ã«ã解æŸããããŸã§åŸ æ©ããŸãã
ãšã©ãŒ7: UnicodeDecodeError
çºçç¶æ³
ãã¡ã€ã«ã®ãšã³ã³ãŒãã£ã³ã°ãäžæãªå Žåã«çºçããŸãã
察åŠæ¹æ³
- ãšã³ã³ãŒãã£ã³ã°ãæå®ããŠãã¡ã€ã«ãéããŸãã
chardet
ã©ã€ãã©ãªã䜿çšããŠãšã³ã³ãŒãã£ã³ã°ãæ€åºããŸãã
äŸ: ãšã³ã³ãŒãã£ã³ã°ãæå®ããŠãã¡ã€ã«ãéã
import os
file_path = "sample_folder/sample_file.txt"
try:
with open(file_path, "r", encoding="utf-8") as f:
content = f.read()
print(content)
except UnicodeDecodeError:
print(f"ãšã©ãŒ: ãã¡ã€ã«ã®ãšã³ã³ãŒãã£ã³ã°ãäžæã§ã ({file_path})")
ãŸãšã
ãããã®ãã©ãã«ã·ã¥ãŒãã£ã³ã°ã掻çšããããšã§ããã¡ã€ã«æäœæã«çºçãããšã©ãŒãå¹ççã«è§£æ¶ããã¹ã¯ãªããã®ä¿¡é Œæ§ãåäžãããããšãã§ããŸãã
9. ãŸãšã
ãã®èšäºã§ã¯ãPythonã䜿ã£ãŠãã©ã«ãå ã®ãã¡ã€ã«åãååŸããæ¹æ³ãåºç€ããå¿çšãŸã§è§£èª¬ããŸãããããããã®ææ³ã®ç¹åŸŽãé©çšå Žé¢ãæ¯ãè¿ããã©ã®æ¹æ³ãéžã¶ã¹ããããŸãšããŸãã
䜿çšããææ³ã®ç¹åŸŽãšäœ¿ãåã
ææ³ | ç¹åŸŽ | é©çšå Žé¢ |
---|---|---|
osã¢ãžã¥ãŒã« | – æšæºã©ã€ãã©ãªã®äžéšã§æ軜ã«äœ¿çšå¯èœã | å°èŠæš¡ãªãã©ã«ãã®åŠçãåºæ¬çãªãã¡ã€ã«ååŸã«æé©ã |
– ååž°çãªæ€çŽ¢ã«ã¯os.walk() ã䜿çšã | ãµããã£ã¬ã¯ããªãå«ãããã¡ã€ã«æäœãå¿ èŠãªå Žåã | |
globã¢ãžã¥ãŒã« | – ã¯ã€ã«ãã«ãŒãã«ããæè»ãªãã¿ãŒã³æ€çŽ¢ãå¯èœã | æ¡åŒµåãååã®æ¡ä»¶ä»ãæ€çŽ¢ãå¹ççã«è¡ãããå Žåã |
– recursive=True ã§ååž°çæ€çŽ¢ãå¯èœã | ãµããã£ã¬ã¯ããªå ã®ç¹å®æ¡ä»¶ã«åèŽãããã¡ã€ã«æ€çŽ¢ã | |
pathlibã¢ãžã¥ãŒã« | – ãªããžã§ã¯ãæåã§ã¢ãã³ãªèšè¿°ãå¯èœã | Python 3.4以éã䜿çšããŠãããå¯èªæ§ãæ±ããããå Žåã |
– rglob() ã§ååž°çãã€çŽæçã«ãã¡ã€ã«ãæ€çŽ¢å¯èœã | ãµããã£ã¬ã¯ããªãå«ããã©ã«ãæäœãç°¡æœã«æžãããå Žåã |
掻çšäŸã®æ¯ãè¿ã
- ãã¡ã€ã«ãªã¹ãã®äœæïŒ
os
ã¢ãžã¥ãŒã«ãglob
ã¢ãžã¥ãŒã«ã掻çšããŠããã©ã«ãå ã®ãã¹ãŠã®ãã¡ã€ã«ããªã¹ãåã - ç¹å®æ¡ä»¶ã§ã®ãã£ã«ã¿ãªã³ã°ïŒæ¡åŒµåãååãæ£èŠè¡šçŸãçšããŠå¿ èŠãªãã¡ã€ã«ã®ã¿ãéžæã
- ãã¡ã€ã«ã®äžæ¬åŠçïŒååŸãããã¡ã€ã«ãªã¹ããçšããŠãå 容ã®èªã¿èŸŒã¿ãæžã蟌ã¿ãå¹çåã
- å¿çšçãªæäœïŒãªããŒã ãããã¯ã¢ããäœæãäžèŠãã¡ã€ã«ã®åé€ãªã©ã
ãã©ãã«ã·ã¥ãŒãã£ã³ã°ã®éèŠæ§
ãã¡ã€ã«æäœãè¡ãéã«çºçãããããšã©ãŒã«ã€ããŠã觊ããŸããã以äžãæèããããšã§ãã¹ã¯ãªããã®ä¿¡é Œæ§ãåäžãããããŸãã
- ãã¡ã€ã«ããã©ã«ãã®ååšç¢ºèªãäºåã«è¡ãã
- ã¢ã¯ã»ã¹æš©éãç¹æ®æåãžã®å¯Ÿå¿ãèæ ®ããã
- 倧éã®ãã¡ã€ã«ãæ±ãéã®ã¡ã¢ãªäžè¶³ãããã©ãŒãã³ã¹ã«æ³šæããã
Pythonã§ã®ãã¡ã€ã«åååŸããã¹ã¿ãŒããå©ç¹
Pythonã䜿ã£ããã©ã«ãå ã®ãã¡ã€ã«åååŸã¯ãå€ãã®ã·ãŒã³ã§æŽ»çšå¯èœã§ããç¹ã«ä»¥äžã®ãããªå©ç¹ããããŸãã
- æ¥åå¹çåïŒå®åäœæ¥ãããŒã¿æŽçã®èªååã
- æè»æ§ïŒå€æ§ãªæ¡ä»¶ã§ãã¡ã€ã«ãæäœå¯èœã
- ã¹ã±ãŒã©ããªãã£ïŒå°èŠæš¡ãã倧èŠæš¡ãªããŒã¿ã»ãããŸã§å¯Ÿå¿å¯èœã
æåŸã«
Pythonã«ã¯ãå€åœ©ãªã¢ãžã¥ãŒã«ãšæ©èœãçšæãããŠããŸããä»å玹ä»ããos
, glob
, pathlib
ã®åã¢ãžã¥ãŒã«ãé©åã«äœ¿ãåããããšã§ããã¡ã€ã«æäœã«ãããå¹çãšç²ŸåºŠãæ倧åã§ããŸãã
ãã®èšäºãéããŠãåå¿è ããäžçŽè ãŸã§ãPythonã§ã®ãã¡ã€ã«æäœã¹ãã«ãé«ããäžå©ãšãªãã°å¹žãã§ãããã²ãå®éã®ãããžã§ã¯ããæ¥åã§å¿çšããPythonã®äŸ¿å©ããäœæããŠãã ããïŒ