0

Taking an online class on python. typed in from what i can tell the exact some code as the prof. but when i run it, i dont get anything back. not even a error. it just starts $ again. nothing happens at all.

import os

def rfile():
file_list = os.listdir(r"C:/home/zorba/Downloads/prank(2)/prank")
print(file_list)

it is a simple code but i cant get it to do anything. not even an error. i run it and nothing happens. im using linux so maybe im using lisdir() wrong. i dont know but i can continue with this lesson without this code working. the location of the file is home/zorba/Downloads/prank (2) and the file under prank (2) is called prank.

3 Answers3

1

You don't just define functions, you have to "call" them:

import os

def rfile():
    file_list = os.listdir(r"C:/home/zorba/Downloads/prank(2)/prank")
    print(file_list)

rfile()  # Actually call the function!!
supermitch
  • 2,062
  • 4
  • 22
  • 28
  • ah! calling the file. Thanks You. Now it is at least giving me errors to work with! thanks! – Zorba-TheStrange Oct 02 '15 at 15:36
  • You're welcome. One thing to look into is the idea of a `main()` function, and using `if __name__ == '__main__':` http://stackoverflow.com/questions/419163/what-does-if-name-main-do – supermitch Oct 02 '15 at 15:45
0

If you are using Linux then most likely the path

C:/home/zorba/Downloads/prank(2)/prank

does not exist

Replace it with Linux path like

/home/someuser/somedir
matcheek
  • 4,887
  • 9
  • 42
  • 73
  • not one path i try works. even just: /home/username/Downloads/ or anything else. it doesn't give me an error either. it just does not do anything. – Zorba-TheStrange Oct 02 '15 at 15:33
  • @Zorba-TheStrange As already pointed out, except for Windows path you also have a problem with the indentation, so to get your snippet to work you need to correct both issues. – matcheek Oct 02 '15 at 15:37
0

glob allows to use * as in Unix, so you can search, for example, for files in a directory staring with pr, like this: pr*.

Try this:

import glob

file_list= glob.glob(r'C:/home/zorba/Downloads/prank(2)/*')
Air
  • 8,274
  • 2
  • 53
  • 88
LetzerWille
  • 5,355
  • 4
  • 23
  • 26