公開:2025-06-13、更新:2025-06-13
10-8「ファイル一覧」 3ページ
マンガ

3
コンテンツ紹介
テキスト原稿
# 3p
__1__
猫野:
まずは
デイレクトリー内の
ファイルや
ディレクトリーの
一覧を得る方法だ
__2__
sys.exec_prefix(Pythonがインストール
されているディレクトリー)内を調べる
main.py
```py
import sys, os
p_dir = sys.exec_prefix
print(p_dir)
files = os.listdir(p_dir)
for file in files:
p = os.path.join(p_dir, file)
if os.path.isfile(p):
print(f'[f] {file}')
if os.path.isdir(p):
print(f'[d] {file}')
```
---
__3__
ディレクトリー内のリスト
os.listdir(パス)
パスを結合
os.path.join(パス, パス)
__4__
パスがファイルか判定
os.path.isfile(パス)
パスがディレクトリーか判定
os.path.isdir(パス)
---
__5__
出力
```python
C:\Users\ユーザー名\AppData\Local\Programs\Python\PythonXXX
[d] DLLs
[d] Doc
︙
[f] LICENSE.txt
[f] NEWS.txt
︙
```
```c
/*
元
C:\Users\yanai\AppData\Local\Programs\Python\Python313
[d] DLLs
[d] Doc
[d] include
[d] Lib
[d] libs
[f] LICENSE.txt
[f] NEWS.txt
[f] python.exe
[f] python3.dll
[f] python312.dll
[f] pythonw.exe
[d] Scripts
[d] tcl
[f] vcruntime140.dll
[f] vcruntime140_1.dll
*/
```