Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get only files in directory? [duplicate]

I have this code:

allFiles = os.listdir(myPath)
for module in allFiles:
    if 'Module' in module: #if the word module is in the filename
        dirToScreens = os.path.join(myPath, module)    
        allSreens = os.listdir(dirToScreens)

Now, all works well, I just need to change the line

allSreens = os.listdir(dirToScreens)

to get a list of just files, not folders. Therefore, when I use

allScreens  [ f for f in os.listdir(dirToScreens) if os.isfile(join(dirToScreens, f)) ]

it says

module object has no attribute isfile

NOTE: I am using Python 2.7

like image 240
user2817200 Avatar asked Jan 27 '14 15:01

user2817200


People also ask

Can File Explorer find duplicate files?

Find duplicate files and remove them with Windows File Explorer. On the lower-left corner beside the Windows icon, enter Indexing Options on the search bar, then click Indexing Options. Click Modify, and then select Show all locations.

How do I filter only files in Linux?

Open the command-line shell and write the 'ls” command to list only directories. The output will show only the directories but not the files. To show the list of all files and folders in a Linux system, try the “ls” command along with the flag '-a” as shown below.

How to get the path of a file in a directory?

You can use System.IO.Path.GetFileName to do this. string [] files = Directory.GetFiles (dir); foreach (string file in files) Console.WriteLine (Path.GetFileName (file)); While you could use FileInfo, it is much more heavyweight than the approach you are already using (just retrieving file paths).

How to get only the filename of a file in C++?

string [] files = Directory.GetFiles (dir); for (int iFile = 0; iFile < files.Length; iFile++) string fn = new FileInfo (files [iFile]).Name; Use this to obtain only the filename.

How do I search for a specific folder or folder?

You could also choose to search a specific directory by clicking the “Add” button on the Include pane and selecting that folder. Be sure to select “Include files and subfolders” option while adding a new folder to ensure CCleaner searches any folders inside the folder you specify, too.

How do I delete a file from a folder?

Try using "*Copy" in the search box of the folder you are working on. Select the files in the search results and delete. Also try * [*] in the search box, sometimes the file names are appended with a number in brackets. Was this reply helpful?


2 Answers

You can use os.path.isfile method:

import os
from os import path
files = [f for f in os.listdir(dirToScreens) if path.isfile(f)]

Or if you feel functional :D

files = filter(path.isfile, os.listdir(dirToScreens))
like image 192
Paulo Bu Avatar answered Oct 18 '22 07:10

Paulo Bu


"If you need a list of filenames that all have a certain extension, prefix, or any common string in the middle, use glob instead of writing code to scan the directory contents yourself"

import os
import glob

[name for name in glob.glob(os.path.join(path,'*.*')) if os.path.isfile(os.path.join(path,name))]
like image 21
juankysmith Avatar answered Oct 18 '22 06:10

juankysmith