37321/how-to-list-only-files-in-a-directory
How can I list only files in a directory ignoring the sub-directories? I am using listdir method but it lists directories also:
import os print (listdir("/home/Papers/"))
This prints directories also. I want it to print only files
Before printing, you can use a check to print the name only if it is a file. Something like this:
import os from os import path print(f for f in os.listdir('/home') if path.isfile(f))
ou are using Python 2.x syntax with ...READ MORE
You can use the reversed function in ...READ MORE
Not sure what your desired output is, ...READ MORE
import os from optparse import OptionParser, Option class MyOption ...READ MORE
You can also use the random library's ...READ MORE
Syntax : list. count(value) Code: colors = ['red', 'green', ...READ MORE
Enumerate() method adds a counter to an ...READ MORE
You can simply the built-in function in ...READ MORE
Well, you are using a complex way. ...READ MORE
You can use the os module to list the ...READ MORE
OR
At least 1 upper-case and 1 lower-case letter
Minimum 8 characters and Maximum 50 characters
Already have an account? Sign in.