0
我使用OSX终端和Python访问shell变量2.7无法从Python脚本
我的shell脚本:
#!/bin/bash
echo Enter the name of the file
read fileName
python gulp.py
我的Python脚本:
import os #import vars
try:
print os.environ["fileName"] #print imported var
except KeyError:
print "Please set the environment variable"
fileTitle=fileName + '.inp'
f=open('fileTitle', 'r')
line=f.readlines()
print line[0]
我没有阳性结果尝试:
import sys
var1=sys.arg[1]
和
import os
os.getenv["fileName"]
外壳和Python脚本是在同一文件夹
预先感谢您!
各地'F =开放(“fileTitle”,“R”),文件名中的单引号'使打开文本文件名'fileTitle',而不是包含您正在查找的值的变量'fileTitle'。将其更改为'f = open(fileTitle,'r')'。当然,除了通过export或cli设置子进程可用的os env外,当然也是如此。 – alvits