I'm a beginner with Python and I'm trying to list the contents of a directory which is defined as a variable but to no avail.
This is the code:
#!/usr/bin/python
import os
location = "/home/itaig/testdir"
command = os.system('ls -l')," location"
My aim is to count the number of files in the location and print the number.
How can it be achieved?
Edit #1:
In bash I'd do ls -l $location | wc -l , what would be the equivalent in Python?
In any case, I've looked at the links from the comments but wasn't able to get it to work... can you please show me an example?
Thanks
os.system("ls -l " + location). But for listing a directory you can use theglobmodule, which is a bit easier to handle than calling a system command.