最新消息:20210917 已从crifan.com换到crifan.org

【已解决】Python脚本用&去后台运行时__file__不起效果

Python crifan 422浏览 0评论
折腾:
【未解决】Win10中上线对接iOS自动抓包工具
期间,但是从输出的路径看:
crifanli@crifanlideMac  ~  cd /Users/crifanli/dev/DevRoot/appcrawler
crifanli@crifanlideMac  ~/dev/DevRoot/appcrawler   master ●  source venv/bin/activate
(venv)  crifanli@crifanlideMac  ~/dev/DevRoot/appcrawler   master ●  python platformIntegration/autoProcessTask.py &
[1] 13696
(venv)  ⚙ crifanli@crifanlideMac  ~/dev/DevRoot/appcrawler   master ●  20200723 03:37:37 autoProcessTask.py:1585 INFO    CurFilePath=platformIntegration/autoProcessTask.py
20200723 03:37:37 autoProcessTask.py:1586 INFO    PlatformIntegrationFolder=platformIntegration
20200723 03:37:37 autoProcessTask.py:1587 INFO    AppCralwerFolder=
20200723 03:37:37 autoProcessTask.py:1588 INFO    TaskFoderRoot=task
20200723 03:37:37 autoProcessTask.py:1589 INFO    WebDriverAgentRoot=iOSAutomation/refer/WebDriverAgent
20200723 03:37:37 autoProcessTask.py:1590 INFO    OutputRootFolder=platformIntegration/output
20200723 03:37:38 autoProcessTask.py:521  INFO    User xxx login OK
20200723 03:37:38 autoProcessTask.py:402  INFO    Update token ok: refreshTimeStr=2020-07-23 16:07:37, expireTimeStr=2020-07-23 21:37:37
20200723 03:37:38 autoProcessTask.py:1465 INFO    Begin process tasks
20200723 03:37:38 autoProcessTask.py:1574 INFO    No tasks, wait 5.0 seconds for new tasks
AppCralwerFolder
等都是空
所以不对。去看看代码:
CurFilePath = __file__
PlatformIntegrationFolder = os.path.dirname(CurFilePath)
AppCralwerFolder = os.path.dirname(PlatformIntegrationFolder)
# /Users/xxx/dev/xxx/crawler/appAutoCrawler/AppCrawler
# AppCrawler project task root folder, used for save task file
# TaskFoderRoot = "/Users/xxx/dev/xxx/crawler/appAutoCrawler/AppCrawler/task"
TaskFoderRoot = os.path.join(AppCralwerFolder, "task")
WebDriverAgentRoot = os.path.join(AppCralwerFolder, "iOSAutomation", "refer", "WebDriverAgent")
# /Users/xxx/dev/xxx/crawler/appAutoCrawler/AppCrawler/iOSAutomation/refer/WebDriverAgent

    logging.info("CurFilePath=%s", CurFilePath)
    logging.info("PlatformIntegrationFolder=%s", PlatformIntegrationFolder)
    logging.info("AppCralwerFolder=%s", AppCralwerFolder)
    logging.info("TaskFoderRoot=%s", TaskFoderRoot)
    logging.info("WebDriverAgentRoot=%s", WebDriverAgentRoot)
    logging.info("OutputRootFolder=%s", OutputRootFolder)
感觉是:
__file__ 没起效果啊
那就暂时去:
换直接用:
cd platformIntegration
python autoProcessTask.py &
试试:
结果问题依旧:
(venv)  ✘ crifanli@crifanlideMac  ~/dev/DevRoot/appcrawler   master ●  cd platformIntegration
(venv)  crifanli@crifanlideMac  ~/dev/DevRoot/appcrawler/platformIntegration   master ●  python autoProcessTask.py&
[1] 14139
(venv)  ⚙ crifanli@crifanlideMac  ~/dev/DevRoot/appcrawler/platformIntegration   master ●  20200723 03:45:33 autoProcessTask.py:1585 INFO    CurFilePath=autoProcessTask.py
20200723 03:45:33 autoProcessTask.py:1586 INFO    PlatformIntegrationFolder=
20200723 03:45:33 autoProcessTask.py:1587 INFO    AppCralwerFolder=
20200723 03:45:33 autoProcessTask.py:1588 INFO    TaskFoderRoot=task
20200723 03:45:33 autoProcessTask.py:1589 INFO    WebDriverAgentRoot=iOSAutomation/refer/WebDriverAgent
20200723 03:45:33 autoProcessTask.py:1590 INFO    OutputRootFolder=output
感觉是__file__ 不起效果?
python background __file__ not work
python – os.path.dirname(__file__) returns empty – Stack Overflow
那去试试
os.path.dirname(os.path.abspath(__file__))
不过感觉此处还是__file__本身是空的?
Unable to obtain actual directory for __file__ Python – Stack Overflow
Get the path of running file (.py) in Python: __file__ | note.nkmk.me
所以还是先去用print打印看看是否有值再说
CurFilePath = __file__
print("CurFilePath=%s" % CurFilePath)
结果:
 CurFilePath=autoProcessTask.py
PlatformIntegrationFolder=
20200723 03:53:46 autoProcessTask.py:1587 INFO    CurFilePath=autoProcessTask.py
20200723 03:53:46 autoProcessTask.py:1588 INFO    PlatformIntegrationFolder=
很明显,此处输出的是:
当前相对路径的文件名
再去回到父级目录试试
(venv)  ✘ crifanli@crifanlideMac  ~/dev/DevRoot/appcrawler/platformIntegration   master ●  cd ..
(venv)  crifanli@crifanlideMac  ~/dev/DevRoot/appcrawler   master ●  python platformIntegration/autoProcessTask.py&
[1] 14491
(venv)  ⚙ crifanli@crifanlideMac  ~/dev/DevRoot/appcrawler   master ●  CurFilePath=platformIntegration/autoProcessTask.py
PlatformIntegrationFolder=platformIntegration
20200723 03:55:23 autoProcessTask.py:1587 INFO    CurFilePath=platformIntegration/autoProcessTask.py
20200723 03:55:23 autoProcessTask.py:1588 INFO    PlatformIntegrationFolder=platformIntegration
20200723 03:55:23 autoProcessTask.py:1589 INFO    AppCralwerFolder=
的确就是:__file__只输出了相对(当前目录的)路径的当前文件名
所以看来是应该去改为:
os.path.abspath(__file__)
代码:
# CurFilePath = __file__
CurFilePath = os.path.abspath(__file__)
print("CurFilePath=%s" % CurFilePath)
结果就可以得到完整路径了:
(venv)  crifanli@crifanlideMac  ~/dev/DevRoot/appcrawler   master ●  python platformIntegration/autoProcessTask.py &
[1] 14690
(venv)  ⚙ crifanli@crifanlideMac  ~/dev/DevRoot/appcrawler   master ●  CurFilePath=/Users/crifanli/dev/DevRoot/appcrawler/platformIntegration/autoProcessTask.py
PlatformIntegrationFolder=/Users/crifanli/dev/DevRoot/appcrawler/platformIntegration
20200723 03:58:19 autoProcessTask.py:1588 INFO    CurFilePath=/Users/crifanli/dev/DevRoot/appcrawler/platformIntegration/autoProcessTask.py
20200723 03:58:19 autoProcessTask.py:1589 INFO    PlatformIntegrationFolder=/Users/crifanli/dev/DevRoot/appcrawler/platformIntegration
20200723 03:58:19 autoProcessTask.py:1590 INFO    AppCralwerFolder=/Users/crifanli/dev/DevRoot/appcrawler
20200723 03:58:19 autoProcessTask.py:1591 INFO    TaskFoderRoot=/Users/crifanli/dev/DevRoot/appcrawler/task
20200723 03:58:19 autoProcessTask.py:1592 INFO    WebDriverAgentRoot=/Users/crifanli/dev/DevRoot/appcrawler/iOSAutomation/refer/WebDriverAgent
20200723 03:58:19 autoProcessTask.py:1593 INFO    OutputRootFolder=/Users/crifanli/dev/DevRoot/appcrawler/platformIntegration/output
【总结】
此处,之前调试时,__file__得到的路径没问题。
去实际作为服务后台运行时:
python platformIntegration/autoProcessTask.py &
结果:
CurFilePath = __file__
__file__ 只输出了相对路径的文件名
导致后续路径不对。
所以为了获取完整的路径,绝对路径,需要改为:
CurFilePath = os.path.abspath(__file__)
才可以。

转载请注明:在路上 » 【已解决】Python脚本用&去后台运行时__file__不起效果

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
92 queries in 0.186 seconds, using 23.43MB memory