【问题标题】:Is there a way to get the working directory of a current python script if it is in One Drive?如果当前 python 脚本在 One Drive 中,有没有办法获取它的工作目录?
【发布时间】:2022-12-09 04:07:29
【问题描述】:

我现有的 python 脚本在 One Drive 中,我想获取它的路径。我已经尝试了所有的解决方案,但都没有奏效。

此代码根本不起作用。

import os
sys.getcwd()

它带来了 C"\users\hkay。我的 python 文件在一个驱动器中。最接近的解决方案是这个one

from inspect import getsourcefile
from os.path import abspath

abspath(getsourcefile(lambda:0)) 

该解决方案似乎将下载的 OneDrive 版本放入临时文件夹中,如下所示,但该文件实际上位于 C:\Users\hkay\OneDrive\Desktop\Project A\ 中的 OneDrive 中

'D:\TEMP\hkay\Temp\ipykernel_179044\1451747622.py'

【问题讨论】:

    标签: python-3.x onedrive os.path working-directory


    【解决方案1】:

    在 stackoverflow link 上查看这个问题。
    它解释了 __file__ 在 python 中的含义。
    总而言之,它返回调用脚本的路径,结合 os 模块,我认为您甚至可以从 One Drive 获取脚本目录。 希望我帮助了你。

    【讨论】:

      猜你喜欢
      • 2020-08-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-14
      相关资源
      最近更新 更多