python os用法以及os.path路径的聚合-分割-遍历方法

写在前面

在python代码中,处理文件经常涉及到路径的操作,os.path提供了路径操作的多种方法。
通过 pydoc os.path 可以查看到

Help on module posixpath in os:

NAME
   posixpath - Common operations on Posix pathnames.

FILE   
/home/hualong/ssd/software/anaconda2/lib/python2.7/posixpath.py

MODULE DOCS
   https://docs.python.org/library/posixpath

DESCRIPTION
   Instead of importing this module directly, import os and refer to
   this module as os.path.  The "os.path" name is an alias for this
   module on Posix systems; on other systems (e.g. Mac, Windows),
   os.path provides the same operations in a manner specific to that
   platform, and is an alias to another module (e.g. macpath, ntpath).
   
   Some of this can actually be useful on non-Posix systems too, e.g.
   for manipulation of the pathname component of URLs.

FUNCTIONS
   abspath(path)  //绝对路径
       Return an absolute path.
   
   basename(p)  //返回路径中最后一个部分,通常也就是文件名了
       Returns the final component of a pathname
   
   commonprefix(m)
       Given a list of pathnames, returns the longest common leading component
   
   dirname(p)
       Returns the directory component of a pathname
   
   exists(path)
       Test whether a path exists.  Returns False for broken symbolic links
   
   expanduser(path)
       Expand ~ and ~user constructions.  If user or $HOME is unknown,
       do nothing.
   
   expandvars(path)
       Expand shell variables of form $var and ${var}.  Unknown variables
       are left unchanged.
   
   getatime(filename)
       Return the last access time of a file, reported by os.stat().
   
   getctime(filename)
       Return the metadata change time of a file, reported by os.stat().
   
   getmtime(filename)
       Return the last modification time of a file, reported by os.stat().
   
   getsize(filename)
       Return the size of a file, reported by os.stat().
   
   isabs(s)
       Test whether a path is absolute
   
   isdir(s)
   Return true if the pathname refers to an existing directory.
   
   isfile(path)
       Test whether a path is a regular file
   
   islink(path)
       Test whether a path is a symbolic link
   
   ismount(path)
       Test whether a path is a mount point
   
   join(a, *p) //路径拼接
       Join two or more pathname components, inserting '/' as needed.
       If any component is an absolute path, all previous path components
       will be discarded.  An empty last part will result in a path that
       ends with a separator.
   
   lexists(path)
       Test whether a path exists.  Returns True for broken symbolic links
   
   normcase(s)
       Normalize case of pathname.  Has no effect under Posix
   
   normpath(path)
       Normalize path, eliminating double slashes, etc.
   
   realpath(filename)
       Return the canonical path of the specified filename, eliminating any
       symbolic links encountered in the path.
   
   relpath(path, start='.')
       Return a relative version of a path
   
   samefile(f1, f2)
       Test whether two pathnames reference the same actual file
   
   sameopenfile(fp1, fp2)
       Test whether two open file objects reference the same file
   
   samestat(s1, s2)
       Test whether two stat buffers reference the same file
       
split(p) //路径分割
       Split a pathname.  Returns tuple "(head, tail)" where "tail" is
       everything after the final slash.  Either part may be empty.
   
   splitdrive(p)
       Split a pathname into drive and path. On Posix, drive is always
       empty.
   
   splitext(p)
       Split the extension from a pathname.
       
       Extension is everything from the last dot to the end, ignoring
       leading dots.  Returns "(root, ext)"; ext may be empty.
   
   walk(top, func, arg)  //路径的遍历
       Directory tree walk with callback function.
       
       For each directory in the directory tree rooted at top (including top
       itself, but excluding '.' and '..'), call func(arg, dirname, fnames).
       dirname is the name of the directory, and fnames a list of the names of
       the files and subdirectories in dirname (excluding '.' and '..').  func
       may modify the fnames list in-place (e.g. via del or slice assignment),
       and walk will only recurse into the subdirectories whose names remain in
       fnames; this can be used to implement a filter, or to impose a specific
       order of visiting.  No semantics are defined for, or required of, arg,
       beyond that arg is always passed to func.  It can be used, e.g., to pass
       a filename pattern, or a mutable object designed to accumulate
       statistics.  Passing None for arg is common.

DATA
   __all__ = ['normcase', 'isabs', 'join', 'splitdrive', 'split', 'splite...
   altsep = None
   curdir = '.'
   defpath = ':/bin:/usr/bin'
   devnull = '/dev/null'
   extsep = '.'
   pardir = '..'
   pathsep = ':'
   sep = '/'
   supports_unicode_filenames = False

(END)



os.path常用方法的使用说明

  • 路径聚合
  • 路径拆分

# -*- coding:utf-8 -*-
import os

## 路径聚合
os.path.join(prefix, file)
os.path.join(dir_path, os.path.join('rgb_images', label)) 
filename=os.path.join('/home/ubuntu/code',  'split_func')
## 稍微注意 字符串前面是否加 '/'

##路径切分  根据需求有 splitext 和 split
#os.path.splitext()将文件名和扩展名分开
#os.path.split()返回文件的路径和文件名

fname,fename=os.path.splitext('/home/ubuntu/python_coding/split_func/split_function.py')
print 'fname is:',fname  ##路径和文件名
print 'fename is:',fename   ##只剩下扩展名
#输出为:
# fname is:/home/ubuntu/python_coding/split_func/split_function
#fename is:.py

#os.path.split()返回文件的路径和文件名
dirname,filename=os.path.split('/home/ubuntu/python_coding/split_func/split_function.py')
print dirname ##前端路径
print filename  ##文件名和扩展名
#输出为:
# /home/ubuntu/python_coding/split_func
#split_function.py

## 如何还不能满足拆分需求,可以使用 字符串切分
#split()函数
#string.split(str="", num=string.count(str))[n]
#str - - 分隔符,默认为所有的空字符,包括空格、换行(\n)、制表符(\t)等。
#num - - 分割次数。
#[n] - - 选取的第n个分片
string = "hello.world.python"
print string.split('.')#输出为:['hello', 'world', 'python']
print(string.split('.',1))#输出为:['hello', 'world.python']
print(string.split('.',1)[0])#输出为:hello
print(string.split('.',1)[1])#输出为:world.python

os.listdir(path) 列出当前路径的所有文件和文件夹

os.rename(old ,new)


#! __*__ coding=utf-8 __*__

import os
import sys
from operator import itemgetter, attrgetter
##相对路径
file_map = '/home/hualong/ssd/datasets/airsim-depth-dataset/rgb_train_map.txt'
## 文件夹前端路径
file_path = '/home/hualong/ssd/datasets/airsim-depth-dataset/'

dir_table = ['001','002','003','004','005','006','007','008','009','010']
cam_type = ['/rgb_images','/depth_images']
cam_table = ['/lc','/mc','/rc']

number = 0
for dir in dir_table:
    for image in cam_type:
        for cam in cam_table:
            dir_name = file_path+dir+image+cam
            count = 0
            # filelist = (os.listdir(dir_name)).sort()
            for file in sorted(os.listdir(dir_name)):
                count += 1
                number += 1
                str_conut = str(count).zfill(4)
                if image == '/rgb_images':
                    image_type = 'colors'
                else:
                    image_type = 'depth'
                os.rename(os.path.join(dir_name, file), os.path.join(dir_name, str_conut+'_{}.png' .format(image_type)))
                if number%100 ==0:
                    print number

这样的目录结构,将lc mc rc下的每一张图片都重命名,并且rgb depth的图片命名后缀不一致
在这里插入图片描述

参考文档

mark_leiliu-CSDN https://blog.csdn.net/T1243_3/article/details/80170006

  • 0
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一銤阳光

希望分享的内容对你有帮助

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值