# -*- coding: utf-8 -*-
"""
Created on Wed Apr 21 11:50:25 2021
获取当前文件的路径
获取当前文件的父目录
获取当前目录的父目录的父目录与datetime1.py拼接
获取路径下所有文件名
@author: xf
"""
import os
def load_file():
# 全局变量
global father_path
# 获取当前文件路径
current_path = os.path.abspath(__file__)
# 获取当前文件的父目录
father_path = os.getcwd()
#上下两句效果一样
#father_path = os.path.abspath(os.path.dirname(current_path) + os.path.sep + ".")
# config.ini文件路径,获取当前目录的父目录的父目录与congig.ini拼接
config_file_path=os.path.join(os.path.abspath(os.path.dirname(current_path) + os.path.sep + ".."),'datetime1.py')
print('当前目录:' + current_path)
print('当前父目录:' + father_path)
print('datetime1.py路径:' + config_file_path)
load_file()
#获取路径下文件夹名
path = father_path # 文件夹路径
def get_dir(path): #获取目录路径
for root,dirs,files in os.walk(path): #遍历path,进入每个目录都调用visit函数,,有3个参数,root表示目录路径,dirs表示当前目录的目录名,files代表当前目录的文件名
for dir in dirs:
#print(dir) #文件夹名
print(os.path.join(root,dir)) #把目录和文件名合成一个路径
#获取路径下文件名
def get_file(path): #获取文件路径
for root, dirs, files in os.walk(path):
for file in files:
print(file) #文件名
#print(os.path.join(root,file))#带路径文件名
get_dir(path)
get_file(path)