os
import os
os.getcwd()
os.listdir(dir_path)
os.listdir('.')
os.path.abspath(path)
os.path.split(os.getcwd())
os.path.split(os.getcwd()+'\\')
os.path.join(path1, path2, ...)
os.path.join('/home/winter/','ww')
os.path.join('/home/winter/','/ww')
os.path.dirname(path)
os.path.basename(path)
os.path.dirname(os.getcwd()) == os.path.split(os.getcwd())[0]
os.path.basename(os.getcwd()) == os.path.split(os.getcwd())[1]
os.path.exists('1.png')
os.path.exists('/1.png')
os.path.exists(os.path.join('.','1.png'))
os.path.exists('./1.png')
os.path.getmtime(path)
os.path.getatime(path)
os.path.getctime(path)
def newest_file(dir):
files = os.listdir(dir)
files.sort(key=lambda f: os.path.getmtime(os.path.join(dir, f)), reverse=True)
return os.path.join(dir, files[0])
newest_file(os.getcwd())
os.mknod(file)
os.mkdir(path)
os.makedirs(path)
glob
glob模块的主要方法就是glob,该方法返回所有匹配的文件路径列表(list);该方法需要一个参数用来指定匹配的路径字符串(字符串可以为绝对路径也可以为相对路径),其返回的文件名只包括当前目录里的文件名,不包括子文件夹里的文件。
files = glob.glob('./ss[1-9].jpg')
print(files)
f = glob.iglob('ss?.jpg')
print(f)
for file in f:
print(file)
获取一个迭代器(iterator)对象,使用它可以逐个获取匹配的文件路径名。与glob.glob()的区别是:glob.glob同时获取所有的匹配路径,而glob.iglob一次只获取一个匹配路径。
['.\\ss1.jpg', '.\\ss2.jpg']
<generator object _iglob at 0x0000023A5147FA98>
ss1.jpg
ss2.jpg