print(os.path.dirname(os.path.abspath(__file__))) #打印当前文件的所在项目路径 print(basedir) 运行结果:
我们可以通过os.path.join()函数将基础目录与相对路径合并。 # 设置基础目录basedir=os.path.abspath(os.path.dirname(__file__))# 获取当前脚本所在目录的绝对路径# 构建完整的文件路径config_path=os.path.join(basedir,'config','config.yaml')# 结合基础目录与相对路径构建配置文件的路径data_path=os.path....
Python BASEDIR 1.找到绝对路径 importosprint(os.path.abspath(__file__))#D:\Python\study\day2\test6.py#绝对路径 2.转为相对路径 __file__为绝对路径 importosimportsys BASE_DIR=os.path.dirname(os.path.dirname(os.path.abspath(__file__))) sys.path.append(BASE_DIR) 3.当前功能函数文件测试...
os.path.dirname(__file__) 返回当前python执行脚本的执行路径(看下面的例子),这里__file__为固定参数 os.path.abspath(file) 返回一个文件在当前环境中的绝对路径,这里file 一参数 os.path.join(basedir,file) 将file文件的路径设置为basedir所在的路径,这里fbasedir和file都为参数 OK,我们不妨看下面的例子。
在/usr/local/scripts 目录下创建一个basedir.py,具体内容如下 # -*- encoding=utf-8 -*- import os, sys print("__file__ = %s" % __file__) print("os.path.realpath(__file__) = %s" % os.path.realpath(__file__)) print("os.path.dirname(os.path.realpath(__file__)) = %s" %...
basedir=os.path.abspath(os.path.dirname(__file__)) (2)设定某个文件的绝对路径 1 static_file_path=os.path.join(basedir,'index.html') 当然,os.path的用法还有很多还多,这里只是列出常用的这三种,并且给出开发环境的一般用法,至于是否非得这样用,完全看每个人自己的思路和方法,这里仅提供参考。
os.path.dirname(__file__)返回当前python执行脚本的执行路径(看下面的例子),这里__file__为固定参数 os.path.abspath(file)返回一个文件在当前环境中的绝对路径,这里file 一参数 os.path.join(basedir,file)将file文件的路径设置为basedir所在的路径,这里fbasedir和file都为参数 ...
(basedir):forfilenameinfilenames:ext = filename.split('.')[-1]#只统计指定的文件类型,略过一些log和cache文件ifextinwhitelist:filelists.append(os.path.join(parent,filename))#统计一个的行数defcountLine(fname):count =0# 把文件做二进制看待,read.forfile_lineinopen(fname,'rb').readlines:...
在macOS上混淆使用路径(path)时,可以通过以下步骤来处理多个版本的Python: 1. 首先,了解macOS上的路径(path)概念。路径是操作系统用来查找可执行文件的一组目录。当在终端中输...
1、config.py import os basedir = os.path.abspath(os.path.dirname(file)) class Config: SECRET_KEY = os.environ.get('SECRET_KEY') or 'hard to guess string' SQLALCHEMY_COMMIT_ON_TEARDOWN = True FLASKY_MAIL_SUBJECT_PREFIX = '[Flasky]' FLASKY_MAIL_SENDER = 'Flasky Adminflasky@example.com...