python常用模块3(os和sys模块)

it2022-05-05  157

import osimport sys# print(os.getcwd())#取当前工作目录# os.chmod("/usr/local",7)#给文件/目录加权限# print(os.chdir(r"e:\byz_code\day2"))#更改当前目录# print(os.curdir)#当前目录# print(os.pardir)#父目录# print(os.mkdir(r"test1))#创建文件夹# print(os.makedirs(r"test1\test2"))#递归创建文件夹,父目录不存在时创建父目录# print(os.removedirs(r"test1\test2"))#递归删除空目录# print(os.rmdir("test1"))#删除指定的文件夹,只能删除空文件夹# print(os.remove(r"E:\byz_code\day4\a.txt"))#删除文件# print(os.listdir('.'))#列出一个目录下的所有文件# os.rename("test","test1")#重命名# print(os.sep)#当前操作系统的路径分隔符# print(r'%s'%os.linesep)#当前操作系统的换行符# print(os.pathsep)#当前系统的环境变量中每个路径的分隔符,linux是:,windows是;# print(os.environ)#当前操作系统的环境变量# print(__file__)#代表当前文件# print(os.path.abspath('bb.py'))#获取绝对路径# print(os.path.dirname)# print(os.path.dirname(os.path.dirname(__file__)))#获取父目录# print(os.path.exists("hhaaa"))#目录/文件是否存在# print(os.path.isfile("bb.py"))#判断是否是一个文件# print(os.path.isdir("/usr/local"))#是否是一个路径# print(os.path.join("root",'hehe','haha','a.log'))#拼接成一个路径# os.path.join("root",'hehe','haha','a.log')# print(os.path.split("/usr/hehe/hehe.txt"))#分割路径和文件名# print(os.path.dirname("/usr/local"))#获取父目录# print(os.path.basename("/usr/local"))#获取最后一级,如果是文件显示文件名,如果是目录显示目录名# print(os.path.exists("/usr/local"))#目录/文件是否存在# print(os.path.isabs("."))#判断是否是绝对路径# print(os.path.isfile("/usr/local"))#判断是否是一个文件# print(os.path.isdir("/usr/local"))#是否是一个路径# print(os.path.join("/root",'hehe','a.sql'))#拼接成一个路径# print(os.path.getatime("len_os.py"))#输出最近访问时间# print(os.path.getmtime("len_os.py"))#输出最近访问时间import sys#sys.argv命令行参数List,第一个元素是程序本身路径# sys.exit('xxxxx')#退出程序,正常退出时exit(0)# print(sys.version) #获取Python解释程序的版本信息#sys.path #返回模块的搜索路径,初始化时使用PYTHONPATH环境变量的值# print(sys.platform) #返回操作系统平台名称def sayHi(name): print('Hi~ %s'%name)if __name__=='__main__':#只有在运行自己这个python文件的时候 #才会执行下面的代码,在别的模块里面导入的时候是不会执行的 print('这是在自己文件里面的时候:',__name__) sayHi('wmh') # 我自测的时候调用

转载于:https://www.cnblogs.com/lazy-cat-home/p/7072563.html


最新回复(0)