zoukankan      html  css  js  c++  java
  • python 文件路径问题

    需要用到 os 模块和 sys 模块

    import os
    print(__file__)# 输出相对路径
    print(os.path.abspath(__file__)) #输出绝对路径
    # D:Project180410genticRahul_Genetic_Programanalysis	est.py
    
    print os.path.dirname(os.path.abspath(__file__)) #返回目录名不要文件名
    # D:Project180410genticRahul_Genetic_Programanalysis
    
    import sys
    
    BASE_DIR = os.path.dirname(os.path.abspath(__file__)) # 设为基本路径
    
    sys.path.append(BASE_DIR) #将路径添加环境变量 

    os.walk目录遍历

    os.walk(top, topdown=True, onerror=None, followlinks=False)
    

    - top是要遍历的目录。 
    - topdown是代表要从上而下遍历还是从下往上遍历。 
    - onerror可以用来设置当遍历出现错误的处理函数(该函数接受一个OSError的实例作为参数),设置为空则不作处理。 
    - followlinks表示是否要跟随目录下的链接去继续遍历,要注意的是,os.walk不会记录已经遍历的目录,所以跟随链接遍历的话有可能一直循环调用下去。

    os.walk返回的是一个3个元素的元组 (root, dirs, files) ,分别表示遍历的路径名,该路径下的目录列表和该路径下文件列表。注意目录列表和文件列表不是具体路径,需要具体路径(从root开始的路径)的话可以用 os.path.join(root,dir)和 os.path.join(root,dir) 。

    ├── a.py
    ├── b.py
    ├── c.py
    ├── dir1
    │   ├── dir4
    │   │   ├── g.py
    │   │   └── h.py
    │   ├── dirx
    │   │   ├── diry
    │   │   │   └── k.py
    │   │   └── z.py
    │   ├── e.py
    │   ├── f.py
    │   └── g.py
    ├── dir2
    │   ├── dira
    │   │   └── dirb
    │   │       └── dirc
    │   │           └── aha.py
    │   ├── k.py
    │   ├── l.py
    │   └── m.py
    └── dir3
        ├── dir5
        │   └── z.py
        ├── x.py
        └── y.py
    
    10 directories, 17 files  

    测试topdown

    当我用 os.walk 遍历这个目录时,程序和输出如下:

    import os
    
    path = '/Users/nisen/Projects/python_advanced_class/test/test_os_walk'
    
    for root, dirs, files in os.walk(path, True):
        print('root: %s' % root)
        print('dirs: %s' % dirs)
        print('files: %s' % files)
        print (' ')
    

      结果如下,从root的路径可以看出遍历是自上而下的:

    root: /Users/nisen/Projects/python_advanced_class/test/test_os_walk
    dirs: ['dir1', 'dir2', 'dir3']
    files: ['a.py', 'b.py', 'c.py']
    
    root: /Users/nisen/Projects/python_advanced_class/test/test_os_walk/dir1
    dirs: ['dir4', 'dirx']
    files: ['e.py', 'f.py', 'g.py']
    
    root: /Users/nisen/Projects/python_advanced_class/test/test_os_walk/dir1/dir4
    dirs: []
    files: ['g.py', 'h.py']
    
    root: /Users/nisen/Projects/python_advanced_class/test/test_os_walk/dir1/dirx
    dirs: ['diry']
    files: ['z.py']
    
    root: /Users/nisen/Projects/python_advanced_class/test/test_os_walk/dir1/dirx/diry
    dirs: []
    files: ['k.py']
    
    root: /Users/nisen/Projects/python_advanced_class/test/test_os_walk/dir2
    dirs: ['dira']
    files: ['k.py', 'l.py', 'm.py']
    
    root: /Users/nisen/Projects/python_advanced_class/test/test_os_walk/dir2/dira
    dirs: ['dirb']
    files: []
    
    root: /Users/nisen/Projects/python_advanced_class/test/test_os_walk/dir2/dira/dirb
    dirs: ['dirc']
    files: []
    
    root: /Users/nisen/Projects/python_advanced_class/test/test_os_walk/dir2/dira/dirb/dirc
    dirs: []
    files: ['aha.py']
    
    root: /Users/nisen/Projects/python_advanced_class/test/test_os_walk/dir3
    dirs: ['dir5']
    files: ['x.py', 'y.py']
    
    root: /Users/nisen/Projects/python_advanced_class/test/test_os_walk/dir3/dir5
    dirs: []
    files: ['z.py']
    

      而当设置os.walk的topdown为False时,结果如下, 可以看出他是自上而下遍历的:

    root: /Users/nisen/Projects/python_advanced_class/test/test_os_walk/dir1/dir4
    dirs: []
    files: ['g.py', 'h.py']
    
    root: /Users/nisen/Projects/python_advanced_class/test/test_os_walk/dir1/dirx/diry
    dirs: []
    files: ['k.py']
    
    root: /Users/nisen/Projects/python_advanced_class/test/test_os_walk/dir1/dirx
    dirs: ['diry']
    files: ['z.py']
    
    root: /Users/nisen/Projects/python_advanced_class/test/test_os_walk/dir1
    dirs: ['dir4', 'dirx']
    files: ['e.py', 'f.py', 'g.py']
    
    root: /Users/nisen/Projects/python_advanced_class/test/test_os_walk/dir2/dira/dirb/dirc
    dirs: []
    files: ['aha.py']
    
    root: /Users/nisen/Projects/python_advanced_class/test/test_os_walk/dir2/dira/dirb
    dirs: ['dirc']
    files: []
    
    root: /Users/nisen/Projects/python_advanced_class/test/test_os_walk/dir2/dira
    dirs: ['dirb']
    files: []
    
    root: /Users/nisen/Projects/python_advanced_class/test/test_os_walk/dir2
    dirs: ['dira']
    files: ['k.py', 'l.py', 'm.py']
    
    root: /Users/nisen/Projects/python_advanced_class/test/test_os_walk/dir3/dir5
    dirs: []
    files: ['z.py']
    
    root: /Users/nisen/Projects/python_advanced_class/test/test_os_walk/dir3
    dirs: ['dir5']
    files: ['x.py', 'y.py']
    
    root: /Users/nisen/Projects/python_advanced_class/test/test_os_walk
    dirs: ['dir1', 'dir2', 'dir3']
    files: ['a.py', 'b.py', 'c.py']
    

      os.path.join()函数用于路径拼接文件路径

    • 会从第一个以”/”开头的参数开始拼接,之前的参数全部丢弃。

    • 以上一种情况为先。在上一种情况确保情况下,若出现”./”开头的参数,会从”./”开头的参数的上一个参数开始拼接

    import os
    
    print("1:",os.path.join('aaaa','/bbbb','ccccc.txt'))
    
    print("2:",os.path.join('/aaaa','/bbbb','/ccccc.txt'))
    
    print("3:",os.path.join('aaaa','./bbb','ccccc.txt'))
    
    # 1: /bbbbccccc.txt
    # 2: /ccccc.txt
    # 3: aaaa./bbbccccc.txt
    

      

  • 相关阅读:
    2013ACM多校联合(1)
    AcDream 1083 完美数 数位DP
    AcDream 1079 郭氏数
    AcDream 1084 同心树 几何
    AcDream 1078 递推数 嵌套循环节+矩阵快速幂
    AcDream 1081 平衡树 Tire树
    ZOJ1455 Schedule Problem 差分约束
    在程序中加载log4net配置,防止其他人看到配置文件
    sqlite错误 The database disk image is malformed database disk image is malformed 可解决
    由于这台计算机没有终端服务器客户端访问许可证,远程会话被中断。请与服务器管理员联系 解决
  • 原文地址:https://www.cnblogs.com/lichunl/p/9117332.html
Copyright © 2011-2022 走看看