Duplicate:
In Python, how do I get the path and name of the file that is currently executing?

Duplicate:在Python中,如何获取当前正在执行的文件的路径和名称?

How do I get the path of a the Python script I am running in? I was doing dirname(sys.argv[0]), however on Mac I only get the filename - not the full path as I do on Windows.

如何获得正在运行的Python脚本的路径?我正在做dirname(sys.argv[0]),但是在Mac上,我只得到文件名,而不是像我在Windows上那样的完整路径。

No matter where my application is launched from, I want to open files that are relative to my script file(s).

无论我的应用程序是从哪里启动的,我都希望打开与我的脚本文件相关的文件。

5 个解决方案

#1


342

os.path.realpath(__file__) will give you the path of the current file, resolving any symlinks in the path. This works fine on my mac.

realpath(__file__)将为您提供当前文件的路径,解析路径中的任何符号链接。这在我的mac上很好用。

更多相关文章

  1. python 处理csv文件的过程对换行符的处理
  2. linux修改文件所属用户和组
  3. Linux的文件权限
  4. linux系统更改目录和文件的权限总结
  5. CentOS7.2 通过nfs设置共享文件夹
  6. linux下查找包含关键字的文件
  7. Linux系统下Tar文件安装方法
  8. Linux学习笔记(九)--RedHat 7.0之用户身份与文件权限、存储结构与
  9. 在Linux用tar归档压缩文件时忽略某些文件和目录

随机推荐

  1. Android 图片加载图片_OOM异常解决
  2. Android corners 圆角属性各个版本之间兼
  3. 系出名门Android(8) - 控件(View)之TextS
  4. Android出现Could not find ***.apk!错误
  5. android EditText中的inputType .
  6. android 环境搭建helloworld
  7. android移动数据上网的开关的实现
  8. 腾讯微博客户端开发视频--若水(第三集、第
  9. Android SDK 快速安装方法
  10. android获取APK签名信息及MD5指纹