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
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.
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.
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).
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.
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.
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?
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))
"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))]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With