【发布时间】:2011-10-10 15:12:24
【问题描述】:
在python中打开当前工作目录的临时文件夹中的可用文件
我试过了
pwdir=os.getcwd()
tempdir=pwdir+"/temp/test.txt"
f=open(tempdir,'r+')
当我打印 tempdir 的路径时,它显示正确,并且文件的内容也被读取。
当我尝试从调用此 python 脚本的 Applescript 中组合此操作时。我收到这样的错误
f=open(pwdir1,'r+')
IOError: [Errno 2] No such file or directory: '//temp/test.txt'" number 1
编辑:
我正在使用 Applescript 中的 Shell 脚本来调用这个 pythonscript
do shell script "/Users/mymac/Documents/'Microsoft User Data'/test.py"
编辑:
Python 代码:
tempdir = os.path.join(os.getcwd(),'temp','htmlinput.html')
print tempdir
with open(tempdir) as f:
html=f.read()
来自终端的 Python 输出:(运行良好)
/Users/mymac/Documents/Microsoft User Data/Outlook Script Menu Items/temp/htmlinput.html
我也能看到文件内容。
Applescript 代码:
do shell script "/Users/mymac/Documents/'Microsoft User Data'/'Outlook Script Menu Items'/new.py"
Applescript 错误:
error "Microsoft Outlook got an error: Traceback (most recent call last):
File \"/Users/mymac/Documents/Microsoft User Data/Outlook Script Menu Items/new.py\", line 12, in <module>
with open(tempdir) as f:
IOError: [Errno 2] No such file or directory: '/temp/htmlinput.html'" number 1
【问题讨论】:
-
@eryksun 同样的问题..更新了问题
-
试试@eryksun 的建议或我在编辑中建议的。 Eryksun,你需要更频繁地发布答案而不是 cmets,这样我才能给你投票。
-
@eryksun os.path.join(os.path.dirname(file), 'temp', 'temp.txt') 有效..谢谢
-
@all 如果这是一个罕见的问题,这值得一票:)
标签: python macos scripting file-io applescript