代码改变世界

OS

2018-08-15 17:50  ZealouSnesS  阅读(181)  评论(0编辑  收藏  举报

 

执行shell命令

os.system('touch test.txt')

 

 

 

 

设置权限为777

os.chmod(path, 0o777)


 os.listdir报windows error:

从命令行读取文件夹的时候文件夹路径里面如果有空格则需要用 ' ' 括起来

否则os.listdir(不存在的文件夹)则会windows error

 

write files:

open("xxx","r") read

open("xxx","w") write,override the file

open("xxx","a") wirte,add to the end of the file

 

do nth. when dir exist and create it when dir doesn't exist

os.makedirs()

(recursion)

 

 

dir/file exist

os.path.exists()

https://www.cnblogs.com/jhao/p/7243043.html

 

 

list all the files/dirs in dir:

mylist=os.listdir

sort:

例如:

    dir ='F:/Home_01/img'#当前目录

    filenames=os.listdir(dir)#filenames存储dir下的所有文件名。

    注意:os.listdir()返回的文件名不一定是顺序的,也就是说结果是不固定的,如下图,则filenames[0]有可能为‘22.jpg’,而不是我们所希望的‘11.jpg’。

    解决办法:

    filenames=os.listdir(dir)

    filenames.sort(key=lambda x:int(x[:-4]))#倒着数第四位'.'为分界线,按照‘.’左边的数字从小到大排序

    此时乱序就变成了顺序:filenames=['11.jpg' , '22.jpg' , '30.jpg'],即filenames[1]='22.jpg';当然可根据自己文件名的特征去决定int(x[:?])中?的值,从哪里去分割排序。

https://blog.csdn.net/qq_22227123/article/details/79903116