寫在前面
在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