赞
踩
import os import sys from pathlib import Path def show(): print(f"---->>>> king ca...") def current_file() -> str: return os.path.realpath(__file__) def current_dir() -> str: return os.path.dirname(current_file()) def set_python_path(): root = Path(current_dir()) sys.path.append(str(root)) #主要是这个函数来解决相对路径问题 def run(): from list.dir_list import list_dir_contents #这行代码也很重要,不能放到code最前面,要确保在set_python_path()之后执行 current_directory = os.getcwd() print(f"当前目录: {current_directory}") # 示例:列出当前目录及其所有子目录的内容 print("\n列出当前目录及其子目录的所有内容:") list_dir_contents(current_directory) # 示例:只列出文件 print("\n只列出文件:") list_dir_contents(current_directory, is_file_only=True) # 示例:只列出目录 print("\n只列出目录:") list_dir_contents(current_directory, is_dir_only=True) show() def main(): set_python_path() #这个很关键,否则运行时会找不到自己py工程子目录下的py模块 run() if __name__ == "__main__": main()
import os def list_dir_contents(startpath, indent=0, is_file_only=False, is_dir_only=False): """ 递归地列出目录内容。 :param startpath: 开始列出的目录路径。 :param indent: 缩进级别,用于递归时显示层级结构。 :param is_file_only: 如果为True,则只列出文件。 :param is_dir_only: 如果为True,则只列出目录。 """ if is_file_only and is_dir_only: print("不能同时只列出文件和只列出目录。") return for root, dirs, files in os.walk(startpath): level = root.replace(startpath, '').count(os.sep) indent_str = ' ' * (indent + level) subindent = ' ' * indent if not is_file_only: for dir in dirs: print(f"{indent_str}[D] {dir}") if not is_dir_only: for file in files: print(f"{indent_str}[F] {file}") # 递归调用以列出子目录的内容(如果需要的话) # 注意:这里我们没有增加indent,因为每个新的os.walk循环已经处理了缩进 # 但你可以根据需要调整indent参数来改变缩进级别
from setuptools import setup, find_packages setup( name='ax_dirtest', version='0.9.9', packages=find_packages(), install_requires=[ # 这里列出你的包依赖项 ], include_package_data=True, entry_points={ 'console_scripts': [ 'ax_dirtest = ax_dirtest.dir_test:main', ], }, )
整个工程代码压缩包:
注意:
python setup.py bdist_wheel
生成的.whl文件在dist目录下
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。