Open jzztf opened 6 years ago
>>> import os >>> os.getcwd() '/home/ztf' >>> os.chdir('/tmp') >>> os.getcwd() '/tmp' >>> os.path.split('/tmp/test.sh') ('/tmp', 'test.sh') >>> (dirname,filename) = os.path.split('/tmp/test.sh') >>> print(dirname) /tmp >>> print(filename) test.sh >>> os.path.join(dirname,filename) '/tmp/test.sh' >>> os.path.splitext('/tmp/test.sh') ('/tmp/test', '.sh') >>> os.path.splitext(filename) ('test', '.sh') >>> (shortname,extension) = os.path.splitext(filename) >>> print(shortname) test >>> print(extension) .sh
可以根据通配符返回当前目录下的文件及目录
>>> import glob >>> glob.glob('tmp/*.sh') ['tmp/loop.sh', 'tmp/test.sh'] >>> for i in glob.glob('tmp/*.sh'): ... print(i) ... tmp/loop.sh tmp/test.sh
python 文件及目录管理
查看目录
glob模块
可以根据通配符返回当前目录下的文件及目录