Parias Lunkamba Mukeba Parias Lunkamba Mukeba - 7 days ago 4x
Python Question

python: i have an error while trying this on my mac using IDLE on mac or terminal

i want to see some info and get info about my os with python as in my tutorial but actually can't run this code:

import os
F = os.popen('dir')

and this :

' Volume in drive C has no label.\n'
F = os.popen('dir') # Read by sized blocks
' Volume in drive C has no label.\n Volume Serial Nu'

os.popen('dir').readlines()[0] # Read all lines: index
' Volume in drive C has no label.\n'
os.popen('dir').read()[:50] # Read all at once: slice
' Volume in drive C has no label.\n Volume Serial Nu'

for line in os.popen('dir'): # File line iterator loop
... print(line.rstrip())

this is the the error for the first on terminal, (on IDLE it return just an '

f = open('dir')
Traceback (most recent call last):
File "", line 1, in
FileNotFoundError: [Errno 2] No such file or directory: 'dir'

I know on mac it should be different but how? to get the same result
using macOS sierra.


os.popen executes a program and returns a file-like object to read the program output. So, os.popen('dir') runs the dir command (which lists information about your hard drive) and gives you the result. open opens a file on your hard drive for reading.

Your problem is that there is no file named dir. You likely wanted f = os.popen(dir) MAC is different than windows and the base directory listing command is ls. But most unixy systems have a dir command that lists files in a different format, so it should work for you.