[{"createTime":1735734952000,"id":1,"img":"hwy_ms_500_252.jpeg","link":"https://activity.huaweicloud.com/cps.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=V1g3MDY4NTY=&utm_medium=cps&utm_campaign=201905","name":"华为云秒杀","status":9,"txt":"华为云38元秒杀","type":1,"updateTime":1735747411000,"userId":3},{"createTime":1736173885000,"id":2,"img":"txy_480_300.png","link":"https://cloud.tencent.com/act/cps/redirect?redirect=1077&cps_key=edb15096bfff75effaaa8c8bb66138bd&from=console","name":"腾讯云秒杀","status":9,"txt":"腾讯云限量秒杀","type":1,"updateTime":1736173885000,"userId":3},{"createTime":1736177492000,"id":3,"img":"aly_251_140.png","link":"https://www.aliyun.com/minisite/goods?userCode=pwp8kmv3","memo":"","name":"阿里云","status":9,"txt":"阿里云2折起","type":1,"updateTime":1736177492000,"userId":3},{"createTime":1735660800000,"id":4,"img":"vultr_560_300.png","link":"https://www.vultr.com/?ref=9603742-8H","name":"Vultr","status":9,"txt":"Vultr送$100","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":5,"img":"jdy_663_320.jpg","link":"https://3.cn/2ay1-e5t","name":"京东云","status":9,"txt":"京东云特惠专区","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":6,"img":"new_ads.png","link":"https://www.iodraw.com/ads","name":"发布广告","status":9,"txt":"发布广告","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":7,"img":"yun_910_50.png","link":"https://activity.huaweicloud.com/discount_area_v5/index.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=aXhpYW95YW5nOA===&utm_medium=cps&utm_campaign=201905","name":"底部","status":9,"txt":"高性能云服务器2折起","type":2,"updateTime":1735660800000,"userId":3}]
os.getcwd():仅仅获得程序运行的当前目录所在位置(如果你在a目录下执行某脚本,无论脚本调用了那些,返回的目录仍然是a)
sys.argv[0] = sys.path[0]
要获得当前执行的脚本的所在目录位置。实际上sys.path是Python会去寻找模块的搜索路径列表,sys.path[0]和sys.argv[0]是一回事,因为Python会自动把sys.argv[0]加入sys.path。
具体来说,如果你在C:\test目录下执行python
getpath\getpath.py,那么os.getcwd()会输出C:\test,sys.path[0]会输出C:\test\getpath。
更特别地,如果你用py2exe模块把Python脚本编译为可执行文件,那么sys.path[0]的输出还会变化:
如果把依赖库用默认的方式打包为zip文件,那么sys.path[0]会输出C:\test\getpath\libarary.zip;
如果在setup.py里面指定zipfile=None参数,依赖库就会被打包到exe文件里面,那么sys.path[0]会输出C:\test\getpath\getpath.exe。
2. 正确的方法
但以上这些其实都不是脚本文件所在目录的位置。
比如C:\test目录下还有一个名为sub的目录;C:\test目录下有getpath.py,sub目录下有
sub_path.py,getpath.py调用sub_path.py;我们在C:\test下执行getpath.py。如果我们在
sub_path.py里面使用sys.path[0],那么其实得到的是getpath.py所在的目录路径C:\test,因为Python虚拟机是从getpath.py开始执行的。如果想得到sub_path.py的路径,那么得这样:
os.path.split(os.path.realpath(__file__))[0]
其中file虽然是所在.py文件的完整路径,但是这个变量有时候返回相对路径,有时候返回绝对路径,因此还要用os.path.realpath()函数来处理一下。也即在这个例子里,os.path.realpath(__file__)输出是C:\test\sub\sub_path.py,而os.path.split(os.path.realpath(__file__))[0]输出才是C:\test\sub。
3. 实例说明
总之,举例来讲,os.getcwd()、sys.path[0] (sys.argv[0])和file的区别是这样的:
假设目录结构是:
复制代码 代码如下:
C:test
|-getpath
|-path.py
|-sub
|-sub_path.py
然后我们在C:\test下面执行python getpath/path.py,这时sub_path.py里面与各种用法对应的值其实是:
os.getcwd() “C:\test”,取的是起始执行目录
sys.path[0]或sys.argv[0] “C:\test\getpath”,取的是被初始执行的脚本的所在目录
os.path.split(os.path.realpath(__file__))[0]
“C:\test\getpath\sub”,取的是file所在文件sub_path.py的所在目录