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 "<stdin>", line 1, in <module>
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的默認路徑:python

 

能夠對 MS-DOS的默認路徑進行修改:shell

 

修改默認路徑:測試

 

 

再次測試:orm

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()
相關文章
相關標籤/搜索