Python Forum
Thread Rating:
  • 1 Vote(s) - 2 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Directory error? Mac
#1
Hi there,

First post here, i've been messing around with Python for a while now and recently decided to do an online class about it. I've been doing fine with all lessons and have been able to figure out/overcome most problems/questions I've ran into but now I'm stuck. In this one we're working with iters and creating a simple code to open a list of all .py files in terminal. The code he used was:

import os
files = os.popen('dir *.py')
fileit = iter(files)
print(next(fileit))
And when he saves it and runs it whatever is equivalent to terminal on a PC, it lists all of the .py files with no question. When I run it i get the follow error message in terminal: 

sh: dir: command not found
Traceback (most recent call last):
  File "/users/james/python/otheriters.py", line 7, in <module>
    print(next(fileit))
StopIteration

I'm very lost and am wondering if the sequence is different on a mac or what?? As I said, I relatively new to Python but have been loving working with it recently and really any help is appreciated. I am using Python 2.7.10 and running in Terminal on a Mac.

Thank you
Reply
#2
dir is purely DOS/Windows command; on Mac (*nix) use ls
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
Reply
#3
(May-02-2017, 08:25 PM)jconway2190 Wrote: print(next(fileit))
This is the format of of Python 3's print() function.
If you're on a Mac, you might have Python 3 already installed, from the command line, type "python3" rather than "python"
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Reply
#4
(May-02-2017, 08:33 PM)sparkz_alot Wrote: This is the format of of Python 3's print() function.
If you're on a Mac, you might have Python 3 already installed, from the command line, type "python3" rather than "python"
This is not the problem - please, take a look at line 2
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Coding error. Can't open directory EddieG 6 1,253 Jul-13-2023, 06:47 PM
Last Post: deanhystad
  Using pyinstaller with .ui GUI files - No such file or directory error diver999 3 3,874 Jun-27-2023, 01:17 PM
Last Post: diver999
  Error about missing directory Led_Zeppelin 3 2,875 Aug-31-2021, 01:37 PM
Last Post: snippsat
  List of error codes to find (and count) in all files in a directory tester_V 8 3,873 Dec-11-2020, 07:07 PM
Last Post: tester_V
  Error With Reading Files In Directory And Calculating Values chascp 2 2,553 Feb-15-2020, 01:57 PM
Last Post: chascp
  Python 2.7 Import error. Directory and filename conflict petcoo00 2 2,466 Feb-02-2020, 08:46 PM
Last Post: snippsat
  pygame.error: /etc/timidity.cfg: No such file or directory lsepolis123 3 4,442 Aug-23-2019, 07:53 AM
Last Post: lsepolis123
  How to fix error code 2 in python, “directory not found”? dav3javu 1 7,165 Apr-03-2019, 04:55 PM
Last Post: Larz60+
  Python Directory and File Create Error basic_learner 1 2,473 Dec-16-2018, 11:03 AM
Last Post: buran
  Changing Directory; Syntax Error DavidRobinsons 1 3,460 Oct-08-2018, 02:13 AM
Last Post: ichabod801

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020