文件操作
file_path=/Users/demon.li/log.txt
分割函数
print os.path.split(file_path)
Output:
('/Users/demon.li', 'log.txt')
#返回文件名
print os.path.basename(file_path)
Output:
log.txt
#返回文件路径
print os.path.dirname(file_path)
Output:
/Users/demon.li
#分离文件名与扩展名
print os.path.splitext(file_path)
Output:
('/Users/demon.li/log', '.txt')
查询函数
print os.path.exists(file_path)
Output:
存在:True
不存在:False
print os.path.isabs(file_path)
Output:
True/False
print os.path.isfile(file_path)
Output:
True/False
print os.path.isdir(file_path)
Output:
True/False
print os.path.islink(file_path)
Output:
True/False
#输出当前工作目录路径
print os.getcwd()
目录操作
dir_path=/Users/demon.li
#输出目录下的所有文件和目录列表
print os.listdir(dir_path)
print os.mkdir(dir_path)
#循环创建目录
print os.makedirs(dir_path)
Other
#与平台相关的路径分隔符
print os.sep
command="cp ~/log.txt ~/log_new.txt"
os.system(command)