eg1:
[root@kooxoo20-180 sersync]# cat test.py
#!/usr/bin/env pythonprint __file__[root@kooxoo20-180 sersync]# python test.py test.py ##me相对路径得到的是相对路径[root@kooxoo20-180 sersync]# python /home/wuxy/sersync/test.py /home/wuxy/sersync/test.py ##绝对路径得到的是绝对路径eg2:
[root@kooxoo20-180 sersync]# cat test.py
#!/usr/bin/env pythonimport osa=os.path.realpath(__file__)print a[root@kooxoo20-180 sersync]# python test.py /home/wuxy/sersync/test.py[root@kooxoo20-180 sersync]# python /home/wuxy/sersync/test.py /home/wuxy/sersync/test.py ##不管怎么执行,得到的都是绝对路径eg3:
[root@kooxoo20-180 sersync]# cat test.py
#!/usr/bin/env pythonimport osa=os.path.realpath(__file__)print aFILE_PATH=os.path.dirname(a)print FILE_PATHFILE_PATH=os.path.dirname(os.path.realpath(__file__)) ##建议使用这种方式print FILE_PATH[root@kooxoo20-180 sersync]# python test.py /home/wuxy/sersync/test.py/home/wuxy/sersync/home/wuxy/sersync ##调用变量和不使用变量的print值都是一样的eg4:
[root@kooxoo20-180 sersync]# cat test.py
#!/usr/bin/env pythonimport osFILE_PATH=os.path.dirname(os.path.realpath(__file__))PYCORE_PATH = os.path.realpath(os.path.join(FILE_PATH, '..', 'pycore'))print PYCORE_PATH[root@kooxoo20-180 sersync]# pwd/home/wuxy/sersync[root@kooxoo20-180 sersync]# python test.py /home/wuxy/pycore##me:os.path.join 用 '/' 将各个路径连接起来。