環境
- Windows 10
- Python 3.10.1
- VSCode
使用するソースコード
以下の公開リポジトリに置いています
GitHub - masayan1126/tao-py-py: pythonリポジトリ
pythonリポジトリ. Contribute to masayan1126/tao-py-py development by creating an account on GitHub.
実装
osモジュールのwalk()を使用する
shared\Application\get_file_system_info_service.py
import os
from shared.Domain.xpath import XPath
# 対象のディレクトリに存在するディレクトリ名とファイル名を取得します
class GetFileSystemInfoService:
def execute(self, xpath: XPath):
return os.walk(xpath.get_path())
packages\get_file_system_info\main.py
import pandas as pd
from shared.Application.get_file_system_info_service import GetFileSystemInfoService
from shared.Domain.xcsv import XCsv
from shared.Domain.xpath import XPath
files = GetFileSystemInfoService().execute(
XPath("C:\\Users\\nishigaki\\jupyter-lab\\shared\\Application")
)
XCsv().output(
"C:\\Users\\nishigaki\\Desktop\\path_lists.csv",
pd.DataFrame(data=files),
)
コンソールへの出力だと結果が見にくいので、pandasでcsv出力したら以下のようになる
ちなみに、for文を用いて以下のように書くと、対象のディレクトリ・対象のディレクトリ内のフォルダ・ファイルが取得できる
for root, dirs, files in files:
print("-" * 10)
print("root:{}".format(root))
print("dirs:{}".format(dirs))
print("files:{}".format(files))
----------
root:C:\Users\nishigaki\jupyter-lab\shared\Application
dirs:['Init', '__pycache__']
files:['check_is_base64_service.py', 'check_is_valid_url_service.py', 'check_regex_service.py', 'decode_url_service.py', 'download_file_service.py', 'download_image_service.py', 'encode_url_service.py', 'find_web_elements_service.py', 'find_web_element_service.py', 'get_file_system_info_service.py', 'make_folder_service.py', 'open_browser_service.py', 'open_desktop_app_service.py', 'open_text_service.py', 'set_webelement_value_service.py']
----------
root:C:\Users\nishigaki\jupyter-lab\shared\Application\Init
dirs:[]
files:['init_chrome_browser_option_service.py']
----------
root:C:\Users\nishigaki\jupyter-lab\shared\Application\__pycache__
dirs:[]
files:['check_is_base64_service.cpython-310.pyc', 'check_is_valid_url_service.cpython-310.pyc', 'check_regex_service.cpython-310.pyc', 'decode_url_service.cpython-310.pyc', 'download_file_service.cpython-310.pyc', 'download_image_service.cpython-310.pyc', 'encode_url_service.cpython-310.pyc', 'find_web_elements_service.cpython-310.pyc', 'find_web_element_service.cpython-310.pyc', 'get_file_system_info_service.cpython-310.pyc', 'make_folder_service.cpython-310.pyc', 'open_browser_service.cpython-310.pyc', 'open_desktop_app_service.cpython-310.pyc', 'open_text_service.cpython-310.pyc', 'set_webelement_value_service.cpython-310.pyc', 'xdecode_url_service.cpython-310.pyc']