I am still a beginner with python and I would like to understand what the following code does.
files = [f for f in os.listdir('E:/figs/test') if os.path.isfile(f)]
imgs = []
#read input
for f in files:
if 'jpg' in f and 'background' not in f:
imgs.append(cv2.imread(f))
print(imgs)
As it can be seen, I have inserted a path to the folder containing the images. However, when I print the content, it is empty. Please, could anyone explain what could be the reason as well as the way for solving it?
It's because os.path.isfile(f) is checking whether f is a file; but f is under E:/figs/text. What you should try is the following:
main_dir = "E:/figs/test"
files = [f for f in os.listdir(main_dir) if os.path.isfile(os.path.join(main_dir, f))]
As this will check the existence of the file f under E:/figs/text.
os.listdir() method in python is used to get the list of all files and directories in the specified directory. If we don’t specify any directory, then list of files and directories in the current working directory will be returned.
You have to use // instead of / in your folder path
Like this:
files = [f for f in os.listdir('E://figs//test') if os.path.isfile(f)]
Try this it may run
Related
I have a list
fileslist=[1.jpg,2.xml,3.png]
I want to search files in list in current working directory
I have tried
listingdir=os.getcwd()
for rootpath,directories,files in os.walk(listingdir):
for file in fileslist:
if file in files:
print("file:{} found".format(file))
I also tried
list=(set(files).intersection(fileslist))
but not worked because of not only one type extentions in files
when I used set it creates a list like following and i don't get the results
f=set(files)
print(f)
#result is
[[1.jpg,2.jpg,....],[1.png,2.png,...],[1.xml,2.xml,.......]]
If you only want to search through the current dir, you can do something like:
files = [f for f in os.listdir() if os.path.isfile(f)]
fileslist = ['1.jpg','2.xml','3.png']
list = (set(files).intersection(fileslist))
Output:
{'1.png'} # it wont always be this, just an example.
You may use os.path.isfile(...). It will check if a certain file exists. It may accept a full path or a filename only (then it will check if the file exists in the current working directory).
import os.path
fileslist=['1.jpg','2.xml','3.png'] # no, it won't work without the quotes!
for f in fileslist:
if os.path.isfile(f):
print("file:{} found".format(f))
Here is my code:
files = [f for f in os.listdir(os.getcwd() + "\\folder") if os.path.isfile(f)]
for file in files:
print("hello")
I am running this from the directory which contains a folder called "folder". This folder has 4 files in it. This should print "hello" four times in my head - but it doesn't.
What have I misunderstood?
PS Do I need to use os.getcwd() here? I figure it would be cleaner to just use a relative path, but that also doesn't work.
With os.path.isfile(f) you're asking if f is a file inside your current directory, not inside folder. Replace your code with:
[f for f in os.listdir(os.path.join(os.getcwd(), "folder")) if os.path.isfile(os.path.join("folder", f))]
I've also taken the liberty of using os.path.join to avoid direct concatenation of file and folder names as strings, since slashes can be a bit iffy.
And for the record, no you don't need to use os.getcwd() here (but I left it there anyways).
I have a list of folders that I need to open and read a .log file.
At first, I did not have the list of folders, so I was scanning all the files in the directory. This will lead to redundancy for what I want to use this for.
path = '/home/User/Test/'
files = []
# r=root, d=directories, f = files
for r, d, f in os.walk(path):
for file in f:
if '.log' in file:
files.append(os.path.join(r, file))
for f in files:
log_file = open(f, 'r')
lines = log_file.readlines()
log_file.close()
Now, I have a list paths which look like ['/home/User/Test/folder_test/Process1/Task1/2019-07-31T10%3A30%3A00+00%3A00', ...., 'final_path']
How can I loop through the paths so it is opening the folder, at then extracting the .log file?
NOTE: I am getting rid of path = '/home/User/Test/' as I have the list of specific directories.
I think you can utilize the glob library for this problem. It allows you to pattern-match your paths.
Say I have a directory.
In this directory there are single files as well as folders.
Some of those folders could also have subfolders, etc.
What I am trying to do is find all of the files in this directory that start with "Incidences" and read each csv into a pandas data frame.
I am able to loop through all the files and get the names, but cannot read them into data frames.
I am getting the error that "___.csv" does not exist, as it might not be directly in the directory, but rather in a folder in another folder in that directory.
I have been trying the attached code.
inc_files2 = []
pop_files2 = []
for root, dirs, files in os.walk(directory):
for f in files:
if f.startswith('Incidence'):
inc_files2.append(f)
elif f.startswith('Population Count'):
pop_files2.append(f)
for file in inc_files2:
inc_frames2 = map(pd.read_csv, inc_files2)
for file in pop_files2:
pop_frames2 = map(pd.read_csv, pop_files2)
You are adding only file name to the lists, not their path. You can use something like this to add paths instead:
inc_files2.append(os.path.join(root, f))
You have to add the path from the root directory where you are
Append the entire pathname, not just the bare filename, to inc_files2.
You can use os.path.abspath(f) to read the full path of a file.
You can make use of this by making the following changes to your code.
for root, dirs, files in os.walk(directory):
for f in files:
f_abs = os.path.abspath(f)
if f.startswith('Incidence'):
inc_files2.append(f_abs)
elif f.startswith('Population Count'):
pop_files2.append(f_abs)
Is there a way to list the files (not directories) in a directory with Python? I know I could use os.listdir and a loop of os.path.isfile()s, but if there's something simpler (like a function os.path.listfilesindir or something), it would probably be better.
This is a simple generator expression:
files = (file for file in os.listdir(path)
if os.path.isfile(os.path.join(path, file)))
for file in files: # You could shorten this to one line, but it runs on a bit.
...
Or you could make a generator function if it suited you better:
def files(path):
for file in os.listdir(path):
if os.path.isfile(os.path.join(path, file)):
yield file
Then simply:
for file in files(path):
...
files = next(os.walk('..'))[2]
Using pathlib in Windows as follow:
files = (x for x in Path("your_path") if x.is_file())
Generates error:
TypeError: 'WindowsPath' object is not iterable
You should rather use Path.iterdir()
filePath = Path("your_path")
if filePath.is_dir():
files = list(x for x in filePath.iterdir() if x.is_file())
Since Python 3.6 you can use glob with a recursive option "**". Note that glob will give you all files and directories, so you can keep only the ones that are files
files = glob.glob(join(in_path, "**/*"), recursive=True)
files = [f for f in files if os.path.isfile(f)]
For the special case of working with files in the current directory, you could do it as a simple one-liner list comprehension:
[f for f in os.listdir(os.curdir) if os.path.isfile(f)]
Otherwise in the more general case, directory paths & filenames have to be joined:
dirpath = '~/path_to_dir_of_interest'
files = [f for f in os.listdir(dirpath) if os.path.isfile(os.path.join(dirpath, f))]
You could try pathlib, which has a lot of other useful stuff too.
Pathlib is an object-oriented library for interacting with filesystem paths. To get the files in the current directory, one can do:
from pathlib import *
files = (x for x in Path(".") if x.is_file())
for file in files:
print(str(file), "is a file!")
This is, in my opinion, more Pythonic than using os.path.
See also: PEP 428.
Using pathlib, the shortest way to list only files is:
[x for x in Path("your_path").iterdir() if x.is_file()]
with depth support if need be.
If you use Python 3, you could use pathlib.
But, you have to know that if you use the is_dir() method as :
from pathlib import *
#p is directory path
#files is list of files in the form of path type
files=[x for x in p.iterdir() if x.is_file()]
empty files will be skipped by .iterdir()
The solution I found is:
from pathlib import *
#p is directory path
#listing all directory's content, even empty files
contents=list(p.glob("*"))
#if element in contents isn't a folder, it's a file
#is_dir() even works for empty folders...!
files=[x for x in contents if not x.is_dir()]