python回到初始位置执行程序_os.popen('python hello_out.py')中Python程序执行时默认的当前路径为MS-DOS CMD的默认路径...

>>> import os

>>> os.getcwd()

'D:\\pythonCode\\pp4e'

>>> os.chdir('Streams')

>>> os.getcwd()

'D:\\pythonCode\\pp4e\\Streams'

>>> open('hell_out.py').read()

Traceback (most recent call last):

File "", line 1, in

FileNotFoundError: [Errno 2] No such file or directory: 'hell_out.py'

>>> open('hello_out.py').read()

"print('Hello shell world')"

>>> pipe = os.popen('python3 hello_out.py')

>>> python3: can't open file 'hello_out.py': [Errno 2] No such file or directory

>>> pipe = os.popen(r'python3 .\Streams\hello_out.py') # 说明Python的默认路径为'D:\\pythonCode\\pp4e'

>>> pipe.read()

'Hello shell world\n'

MS-DOS的默认路径:

5908ee30a1972188dcb829ce3ee62001.png

可以对 MS-DOS的默认路径进行修改:

719c288b8fb9d6a80ce123745bb138ce.png

ca5006a3d4eca97a242a026bc1c3ae94.png

4e137f19027965fc10bd3b6b22cc27fb.png

修改默认路径:

8c2822fffa4f12e4066fdf7582f2ab7b.png

再次测试:

D:\pythonCode\pp4e\Streams>python3

Python 3.4.3 (v3.4.3:9b73f1c3e601, Feb 24 2015, 22:44:40) [MSC v.1600 64 bit (AM

D64)] on win32

Type "help", "copyright", "credits" or "license" for more information.

>>> import os

>>> os.getcwd()

'D:\\pythonCode\\pp4e\\Streams'

>>> open('hello_out.py').read()

"print('Hello shell world')"

>>> pipe = os.popen('python3 hello_out.py')

>>> pipe.read()

'Hello shell world\n'

>>> pipe.close()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值