我有以下代碼:如何訪問在函數中聲明的變量?
SCRIPT1
def encoder(input_file):
# a bunch of other code
# some more code
# path to output of above code
conv_output_file = os.path.join(input_file_gs, output_format)
subprocess.run(a terminal file conversion runs here)
if __name__ == "__main__":
encoder("path/to/file")
這是我嘗試導入以及如何將其設置爲SCRIPT2。
SCRIPT2
from script1 import encoder
# some more code and imports
# more code
# Here is where I use the output_location variable to set the input_file variable in script 2
input_file = encoder.conv_output_file
我所試圖做的是另一種python3文件中使用的變量output_location。所以,我可以告訴SCRIPT2到哪裏尋找它嘗試沒有硬編碼處理文件
我每次運行該腳本雖然我得到以下錯誤:
NameError: name 'conv_output_file' is not defined
爲什麼你不能''在函數中返回conv_output_file'? – idjaw
每種方法中的問題/錯誤是什麼?添加更多的代碼到目前爲止你已經嘗試 –
@idjaw我已經試過這個,但我仍然得到不斷收到'conv_output_file沒有定義錯誤。「# – EliC