Python 中当前位置以及目录文件遍历操作

当前位置

  • print(os.path.dirname(__file__))
    • 其中 dirname 会选择目录(文件夹),"__file__" 内置变量,标识当前编写文件位置
  • print(os.getcwd())

目录遍历

import glob

for filename in glob.glob(rootdir+'*/*.md'):
    print(filename,end='\n new_way')
import os

rootdir=os.path.dirname(__file__)+'./TranslateProject/sources/'
for parent,dirnames,filenames in os.walk(rootdir):    #三个参数:分别返回1.父目录 2.所有文件夹名字(不含路径) 3.所有文件名字
    for filename in filenames:                        #输出文件信息
        print(os.path.join(parent,filename))          #输出文件路径信息

Python 中当前位置以及目录文件遍历操作

相关文章:

  • 2021-09-03
  • 2022-12-23
  • 2021-06-06
  • 2021-11-17
  • 2021-06-27
  • 2021-07-22
  • 2021-09-22
  • 2021-12-04
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-08-10
  • 2021-10-10
相关资源
相似解决方案