read all .jack files in python

classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

read all .jack files in python

dmoeller
This post was updated on .
I tried looking online but I don't understand how to load all .jack files from a directory. I want to do something like:

for ('*.jack') in (path):
    jackFile = open('.jack', r)

Please help me understand how to do this. Thank you
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: read all .jack files in python

cadet1620
Administrator
Here's a quick example of reading a directory and filtering for file extension.
[D:/TECS/projects/09/Square]
% cat d:/tmp/test.py

import os
dir_name = '.'      # ( should use argument from command line)
for file_name in os.listdir(dir_name):
    if file_name[-5:] == '.jack':
        out_name = file_name[:-5] + '.vm'
        print(file_name, '->', out_name)

[D:/TECS/projects/09/Square]
% d:/tmp/test.py
Main.jack -> Main.vm
Square.jack -> Square.vm
SquareGame.jack -> SquareGame.vm
[D:/TECS/projects/09/Square]
% ls
Main.jack  OS\         Square.hack  Square.lst  SquareGame.jack
Main.vm    Square.asm  Square.jack  Square.vm   SquareGame.vm
[D:/TECS/projects/09/Square]
%
(For code that is intended to be cross-platform portable you should use the os.path functions that know about OS specific file naming conventions.)

--Mark
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: read all .jack files in python

dmoeller
OK i programmed that. when i try to open(filename, 'rU') and filename in this case would be "Main.jack" i get

File "JackTokenizer.py", line 6, in <module> class JackTokenizer:
File "JackTokenizer.py", line 19, in JackTokenizer jackFile = open(file, 'rU') filenotfounderror: [Errno 2] no such file or directory: "Main.jack"

but it is there. I can see it. it is in the nand2tetris folder under projects\10\arraytest
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: read all .jack files in python

cadet1620
Administrator
My guess is that the "current working directory" is wrong. Try adding
    print(os.getcwd())
to see where it is.

How are you running your Python program?

--Mark
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: read all .jack files in python

dmoeller
i use the command prompt and move to the directory of my JackTokenizer.py. Then i use the command python JackTokenizer.py and it interprets my code from there
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: read all .jack files in python

dmoeller
so i added the print os.getcwd and it prints the directory 1 folder out of where i am aiming. meaning:

C:\users\schuylab\desktop\nand2tetris\projects\10\ArrayTest is where I am trying to navigate to
C:\users\schuylab\desktop\nand2tetris\projects\10               is what prints
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: read all .jack files in python

cadet1620
Administrator
You need to pass the directory name into listdir, as in
    dir_name = 'ArrayTest'
    os.listdir(dir_name)

My example was listing files in the current directory (the "." in dir_name='.')

--Mark
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: read all .jack files in python

dmoeller
this is what I have

        print ('please enter a file or directory path.')
        path = input()
        for file in os.listdir(path):
                if file[-5:]=='.jack':
                        print(os.getcwd())

I thouth input() was a cmd command

so in my case path = C:\users\.....\10\ArrayTest
but only C:\...\10 prints
Loading...