蓝绝

博客园 首页 新随笔 联系 订阅 管理

 

import  os.path
print('1.',os.path.abspath('demo13.py'))                              #获取文件或目录绝对路径
print('2.',os.path.exists('demo13.py'),os.path.exists('demo18.py'))   #判断文件和目录是否存在
print('3.',os.path.join('E:\\Python','demo13.py'))                    #将目录与目录或文件名拼接群起来
print('4.',os.path.split('E:\\vipython\\chap2\\demo13.py'))           #文件的路径和文件名字进行拆分
print('5.',os.path.splitext('demo13.py'))                             #分离文件名和扩展名
print('6.',os.path.basename('E:\\vippython\\chap2\\demo13.py'))       #从一个目录中提取文件名
print('7.',os.path.dirname('E:\\vippython\\chap2\\demo13.py'))        #从一个路径中提取文件路径,不包括文件名
print('8.',os.path.isdir('E:\\PycharmProjects\\pythonProject\\demon1\\chap2\\demo13.py'))
                                                                      #用与判断是否为路径,其中demo13为文件,所以False
F:\python3\python_3.8.3\python.exe E:/PycharmProjects/pythonProject/demon1/chap2/demo17.py
1. E:\PycharmProjects\pythonProject\demon1\chap2\demo13.py
2. True False
3. E:\Python\demo13.py
4. ('E:\\vipython\\chap2', 'demo13.py')
5. ('demo13', '.py')
6. demo13.py
7. E:\vippython\chap2
8. False

进程已结束,退出代码0
#练习题,列出指定目录下的所有py文件
#列出指定目录下的所有py文件
import  os
path=os.getcwd()                  #获取当前工作目录
lst=os.listdir(path)              #获取指定文件下的文件和目录信息
for filename in lst:
    if filename.endswith('.py'):   #后面有 .py 的文件
        print(filename)
F:\python3\python_3.8.3\python.exe E:/PycharmProjects/pythonProject/demon1/chap2/demo18.py
demo1.py
demo13.py
demo17.py
demo18.py
demo2.py
demo3.py
demo4.py
demo5.py
demo6.py
demo7.py
demo8.py
demo9.py

进程已结束,退出代码0

#重要方法wark(path) 方法


import  os
path=os.getcwd()
lst_files=os.walk(path)
for dirpath,dirname,filename in lst_files:
    '''print(dirpath)                                           #文件夹的路径
    print(dirname)                                              #dirpath目录下的子文件夹列表                                                                             
    print(filename)                                             #文件
    print('-------------------------------------')'''
    for dir in dirname:
        print(os.path.join(dirpath,dir))

    for file in filename:
        print(os.path.join(dirpath,file))
    print('----------------------------------')
F:\python3\python_3.8.3\python.exe E:/PycharmProjects/pythonProject/demon1/chap2/demo19.py
E:\PycharmProjects\pythonProject\demon1\chap2\newdir
E:\PycharmProjects\pythonProject\demon1\chap2\newdir2
E:\PycharmProjects\pythonProject\demon1\chap2\a.txt
E:\PycharmProjects\pythonProject\demon1\chap2\copylogo.png
E:\PycharmProjects\pythonProject\demon1\chap2\d.txt
E:\PycharmProjects\pythonProject\demon1\chap2\demo1.py
E:\PycharmProjects\pythonProject\demon1\chap2\demo13.py
E:\PycharmProjects\pythonProject\demon1\chap2\demo17.py
E:\PycharmProjects\pythonProject\demon1\chap2\demo18.py
E:\PycharmProjects\pythonProject\demon1\chap2\demo19.py
E:\PycharmProjects\pythonProject\demon1\chap2\demo2.py
E:\PycharmProjects\pythonProject\demon1\chap2\demo20.py
E:\PycharmProjects\pythonProject\demon1\chap2\demo3.py
E:\PycharmProjects\pythonProject\demon1\chap2\demo4.py
E:\PycharmProjects\pythonProject\demon1\chap2\demo5.py
E:\PycharmProjects\pythonProject\demon1\chap2\demo6.py
E:\PycharmProjects\pythonProject\demon1\chap2\demo7.py
E:\PycharmProjects\pythonProject\demon1\chap2\demo8.py
E:\PycharmProjects\pythonProject\demon1\chap2\demo9.py
E:\PycharmProjects\pythonProject\demon1\chap2\logo.png
E:\PycharmProjects\pythonProject\demon1\chap2\开发商logo组合(1).png
----------------------------------
E:\PycharmProjects\pythonProject\demon1\chap2\newdir\sub
E:\PycharmProjects\pythonProject\demon1\chap2\newdir\1.py
E:\PycharmProjects\pythonProject\demon1\chap2\newdir\2.py
----------------------------------
E:\PycharmProjects\pythonProject\demon1\chap2\newdir\sub\sub.py
----------------------------------
----------------------------------

进程已结束,退出代码0

 


 

 
posted on 2022-10-05 21:38  蓝绝  阅读(33)  评论(0编辑  收藏  举报