首页 > 其他 > 详细

os模块的常用方法使用

时间:2020-07-13 21:59:15      阅读:61      评论:0      收藏:0      [点我收藏+]
‘‘‘
@Descos模块的常用方法使用
os.getcwd()
os.path.exists(dir)
os.listdir(dir)
os.path.join(dir, file)
os.path.isfile(fullpath)
os.path.isdir(fullpath)
os.path.dirname(dir) 可用于跳转到【上一级】目录
‘‘‘

import os

class OsModules:
dirs = "F:\\cs_auto\\DisposaAssetsTest"

def osPathExists(self, dir):
print("当前路径:", os.getcwd())
# 当前文件所在路径:
casepath = os.path.dirname(__file__)
print("casepath===", casepath)
# 当前路径的上一级路径:
basepath = os.path.dirname(casepath)
print("basepath===", basepath)
if os.path.exists(dir):
files = os.listdir(dir)
print(dir, "中包含的目录有:", files)
fullpath = os.path.join(dir, files[0])
print("目录中第一个文件(或目录)的完整路径是:", fullpath)
if os.path.isfile(fullpath):
print(fullpath, "是一个文件")
elif os.path.isdir(fullpath):
print(fullpath, "是一个目录")
else:
print(fullpath, "啥也不是")
else:
print(dir, "不是个目录")

if __name__ == ‘__main__‘:
OsModules.osPathExists("self", OsModules.dirs)

os模块的常用方法使用

原文:https://www.cnblogs.com/jona-test/p/13295574.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!