OS.path用法

1 返回path规范化的绝对路径

  1. >>>os.path.abspath('train.py')
  2. 'C:\\Users\\Administrator\\Desktop\\fastercnn\\faster-rcnn-pytorch-master\\train.py'

2将path分割成目录和文件名二元组返回。

os.path.split(path)

>>>path='C:\\Users\\Administrator\\Desktop\\fastercnn\\faster-rcnn-pytorch-master\\train.py'
>>>os.path.split(path)

('C:\\Users\\Administrator\\Desktop\\fastercnn\\faster-rcnn-pytorch-master', 'train.py')

3 将多个路径组合后返回,第一个绝对路径之前的参数将被忽略。

os.path.join(path1[, path2[, …]])
os.path.join(str+str+…)

4 返回脚本的绝对路径

os.path.abspath(file)

5 返回path的父路径

os.path.dirname()

>>>os.path.dirname(path)

'C:\\Users\\Administrator\\Desktop\\fastercnn\\faster-rcnn-pytorch-master'

6 返回脚本的文件名称

os.path.basename(file)

7 返回路径下所有文件

>>>path='C:\\Users\\Administrator\\Desktop\\fastercnn\\faster-rcnn-pytorch-master'
>>>os.listdir(path)

['.idea', '.temp_files', '1.py', '2007_test.txt', '2007_train.txt', '2007_val.txt', 'frcnn.py', 'get_dr_txt.py', 'get_gt_txt.py', 'get_map.py', 'img', 'input', 'LICENSE', 'logs', 'model_data', 'nets', 'output_test_image', 'predict.py', 'README.md', 'recorder', 'results', 'tensorboard', 'test_image', 'train.py', 'trainer.py', 'utils', 'video.py', 'voc2frcnn.py', 'VOCdevkit', 'voc_annotation.py', '__pycache__', '常见问题汇总.md']

8 返回当前工作目录

os.getcwd()

>>>print(os.getcwd())

C:\Users\Administrator\Desktop\fastercnn\faster-rcnn-pytorch-master\recorder

9 切换当前工作目录

os.chdir()

print(os.getcwd())
os.chdir('C:\\Users\\Administrator\\Desktop\\fastercnn\\faster-rcnn-pytorch-master')
print(os.getcwd())

C:\Users\Administrator\Desktop\fastercnn\faster-rcnn-pytorch-master\recorder
C:\Users\Administrator\Desktop\fastercnn\faster-rcnn-pytorch-master

10 创建目录

使用os.mkdir()创建目录,path为路径,可以是绝对路径和相对路径。mode为创建的模式。

os.mkdir(path[, mode])

由于反斜杠在Python中被认为是转义标记,为在windows中确保万无一失,应以原始字符串的方式指定路径,即在开头的单引号前加上r

11 批量改文件名

os.rename(src, dst)
# src--要修改的目录名
# dst--修改后的目录名
# 该方法没有返回值
import os

src = os.getcwd()
image_path = src + '\\Images\\'
print(src)
print(image_path)
image_list = os.listdir(image_path)
for image_num,image_ids in enumerate(image_list):
    os.rename(image_path+image_ids,image_path+str(image_num)+'.png')

12 生成文件树

def walk(cwd = '/kaggle'):
    for root, dirs, files in os.walk(cwd):
        # print(root)  # 打印当前目录下的所有子目录的绝对路径
        # print(dirs)  # 打印每个目录下的子目录列表
        # print(files)  # 打印所有目录下文件列表
        print(root)
        for file in files:
            print('\t' + file)


walk()

路径

路径中 斜杠/和反斜杠\ 的区别
路径中使用斜杠/和反斜杠\的区别到底是什么。查阅了一些资料后可知。
Unix使用斜杆/ 作为路径分隔符,而web应用最新使用在Unix系统上面,所以目前所有的网络地址都采用 斜杆/ 作为分隔符。
Windows由于使用 斜杆/ 作为DOS命令提示符的参数标志了,为了不混淆,所以采用 反斜杠\ 作为路径分隔符。所以目前windows系统上的文件浏览器都是用 反斜杠\ 作为路径分隔符。随着发展,DOS系统已经被淘汰了,命令提示符也用的很少,斜杆和反斜杠在大多数情况下可以互换,没有影响。
知道这个背景后,可以总结一下结论:
(1)浏览器地址栏网址使用 斜杆/ ;
(2)windows文件浏览器上使用 反斜杠\ ;
(3)出现在html url() 属性中的路径,指定的路径是网络路径,所以必须用 斜杆/ ;

1 <div style="background-image:url(/Image/Control/title.jpg); background-repeat:repeat-x; padding:10px 10px 10px 10px"></div>
2 // 如果url后面用反斜杠,就不会显示任何背景

(4)出现在普通字符串中的路径,如果代表的是windows文件路径,则使用 斜杆/ 和 反斜杠\ 是一样的;如果代表的是网络文件路径,则必须使用 斜杆/ ;

1 <img src=".\Image/Control/ding.jpg" /> // 本地文件路径,/ 和 \ 是等效的
2 <img src="./Image\Control\cai.jpg" />
3 <img src="http://hiphotos.baidu.com/yuhua522/pic/item/01a949c67e1023549c163df2.jpg" /> // 网络文件路径,一定要使用 斜杆/

(5)转义字符。下面两种路径都对,第二行通过转义字符””,使得各编译器解析得到的都是””

1 fw=new FileWriter("C:/Users/020248/Desktop/test/record.txt");
2 fw=new FileWriter("C:\\Users\\020248\\Desktop\\test\\record.txt");

(6)目录
./SRC/ 这样写表示,当前目录中的SRC文件夹;
../SRC/ 这样写表示,当前目录的上一层目录中SRC文件夹;
/SRC/ 这样写表示,项目根目录(可以只磁盘根目录,也可以指项目根目录,具体根据实际情况而定)