将工作文件夹设置为我的脚本所在的位置 Python
Setting a working folder to where my script lives in Python
我正在寻找一种在我的脚本开头设置工作文件夹的简单方法,以避免每次我从另一台机器 运行 我的脚本时都必须更改它。抱歉,如果这很愚蠢,但我必须深入研究奇怪的功能才能找到答案 - 而且我不会游泳。
我会用它来将所有图像保存到同一个特定文件夹。
我的看法:
#import stuff
filepath=raw_input('Provide the working directory: ')
#do stuff
plt.savefig(filepath+'\image.jpg', dpi=2000,bbox='tight')
编辑
我有许多文件要保存在一个特定目录中,与我的脚本所在的目录相同。但它是这样指定的:C:\Users\User\Desktop\N_Scripts
它在我自己的电脑上,但我经常在不同的机器上工作。如何使用 raw_input
?
之类的内容在脚本开头预设所有图像所在的目录
感谢您的帮助!
to save in one specific directory, the same where my script lives
要将 Python 脚本中的当前工作目录更改为脚本所在的目录:
import os
os.chdir(get_script_dir())
其中 get_script_dir()
is defined here.
But then, once you use os.chdir, how do I modify the argument of plt.savefig to make sure my images are saved where my script is?
Would it just be plt.savefig(os.chdir(get_script_dir())+'\image.jpg)?
使用plt.savefig('image.jpg')
.
我正在寻找一种在我的脚本开头设置工作文件夹的简单方法,以避免每次我从另一台机器 运行 我的脚本时都必须更改它。抱歉,如果这很愚蠢,但我必须深入研究奇怪的功能才能找到答案 - 而且我不会游泳。
我会用它来将所有图像保存到同一个特定文件夹。
我的看法:
#import stuff
filepath=raw_input('Provide the working directory: ')
#do stuff
plt.savefig(filepath+'\image.jpg', dpi=2000,bbox='tight')
编辑
我有许多文件要保存在一个特定目录中,与我的脚本所在的目录相同。但它是这样指定的:C:\Users\User\Desktop\N_Scripts
它在我自己的电脑上,但我经常在不同的机器上工作。如何使用 raw_input
?
感谢您的帮助!
to save in one specific directory, the same where my script lives
要将 Python 脚本中的当前工作目录更改为脚本所在的目录:
import os
os.chdir(get_script_dir())
其中 get_script_dir()
is defined here.
But then, once you use os.chdir, how do I modify the argument of plt.savefig to make sure my images are saved where my script is?
Would it just be plt.savefig(os.chdir(get_script_dir())+'\image.jpg)?
使用plt.savefig('image.jpg')
.