I am seeing a strange and frustrating behavior in Google App Engine. I am launching google app engine from a particular folder like this: (dev_appserver .) In this folder I have a folder called data with a few subfolders under it (one of them is json_folder which contains a file temp.json). From inside my main python src that contains the MainHandler class I do this:
print os.getcwd(), os.path.isfile("data/json_folder/temp.json")
It prints the expected cwd and False even though the json_folder/temp.json exists and if I launch a regular python shell from the same directory it correctly prints True. Why does it work in regular Python but not in GAE python?
I also tried the following. I walk through my current dir and list the subfolders under data but the isdir() returns false even for directories! Why is python thinking they are not directories? ls -al shows them to be directories it makes no sense:
(this prints json_folder, False as one of the outputs, all subfolders are returning False):
for root, dirs, files in os.walk("data"):
for file in files:
print file, os.path.isdir(file)
You need to os.path.join the path, it works if you only use files from the cwd as you are in the same directory as the file, once you hit a file folder outside the cwd you are checking if the file from that outside directory is a file in your cwd, you also want isfile to check for files:
print file, os.path.isfile(os.path.join(root, file))
If you want to check for directories you need to iterate over dirs not files:
for root, dirs, files in os.walk("data"):
for _dir in dirs:
print _dir, os.path.isdir(os.path.join(root, _dir)
Related
So my program search_file.py is trying to look for .log files in the directory it is currently placed in. I used the following code to do so:
import os
# This is to get the directory that the program is currently running in
dir_path = os.path.dirname(os.path.realpath(__file__))
# for loop is meant to scan through the current directory the program is in
for root, dirs, files in os.walk(dir_path):
for file in files:
# Check if file ends with .log, if so print file name
if file.endswith('.log')
print(file)
My current directory is as follows:
search_file.py
sample_1.log
sample_2.log
extra_file (this is a folder)
And within the extra_file folder we have:
extra_sample_1.log
extra_sample_2.log
Now, when the program runs and prints the files out it also takes into account the .log files in the extra_file folder. But I do not want this. I only want it to print out sample_1.log and sample_2.log. How would I approach this?
Try this:
import os
files = os.listdir()
for file in files:
if file.endswith('.log'):
print(file)
The problem in your code is os.walk traverses the whole directory tree and not just your current directory. os.listdir returns a list of all filenames in a directory with the default being your current directory which is what you are looking for.
os.walk documentation
os.listdir documentation
By default, os.walk does a root-first traversal of the tree, so you know the first emitted data is the good stuff. So, just ask for the first one. And since you don't really care about root or dirs, use _ as the "don't care" variable name
# get root files list.
_, _, files = next(os.walk(dir_path))
for file in files:
# Check if file ends with .log, if so print file name
if file.endswith('.log')
print(file)
Its also common to use glob:
from glob import glob
dir_path = os.path.dirname(os.path.realpath(__file__))
for file in glob(os.path.join(dir_path, "*.log")):
print(file)
This runs the risk that there is a directory that ends in ".log", so you could also add a testing using os.path.isfile(file).
I want to write a shell script that aims to traverse a root directory C:\xyz and find whether a sub directory "Demo" is available in any directories or not. If "Demo" is available & not empty, print 'OK', if "Demo" directory is empty, print 'NOT_OK'.
(Assuming you want a Python script to be executed in the terminal and not an actual shell script)
You can use os.walk to recursively list all the files and subdirectories in a given directory. This creates a generator with items in the form ("path/from/root", [directories], [files]) which you can then check whether any of those (a) is the desired Demo directory, and (b) is not empty.
import os
exists_and_not_empty = any(path.endswith("/Demo") and (dirs or files)
for (path, dirs, files) in os.walk(root_dir))
This will also return True if the target directory is contained directly in the root directory, or if it is nested deeper in subdirectories of subdirectories. Not entirely clear from your question if this is intended, or if the target directory has to be in a direct subdirectory of the root directory.
To see if there are any directories in folder xyz that contains another directory named 'Demo', and if so, whether or not 'Demo' is empty, you can use the glob module
from glob import glob
if glob("C:\\xyz\\**\\Demo\\*", recursive=True):
print("OK")
else:
print("NOT_OK")
I need to print a list of all files in sub-directories of the directory "H:\Reference_Archive\1EastRefsJan2014". I am currently using the code:
for root, dirs, files in os.walk("H:\Reference_Archive\1EastRefsJan2014"):
for name in files:
print os.path.join(root, name)
The code works and I get a long list of files if I run it only on the root directory ("H:\Reference_Archive"), but when I try to run it on the sub-directory as it is written above, nothing is returned or printed. The path that is written above contains several more sub-directories which all contain files. I have double checked that I have the pathway correct.
try this, you omitted dirs
for root, dirs, files in os.walk("H:\Reference_Archive\1EastRefsJan2014"):
for name in files:
print os.path.join(root, name)
Finally figured out that the os.walk function was not working with my folder because the folder name started with a number. Once I changed the name of the folder, it worked properly.
I'm relatively new to python and I'm trying my hand at a weekend project. I want to navigate through my music directories and get the artist name of each music file and export that to a csv so that I can upgrade my music collection (a lot of it is from when I was younger and didn't care about quality).
Anyway, I'm trying to get the path of each music file in its respective directory, so I can pass it to id3 tag reading module to get the artist name.
Here is what I'm trying:
import os
def main():
for subdir, dirs, files in os.walk(dir):
for file in files:
if file.endswith(".mp3") or file.endswith(".m4a"):
print(os.path.abspath(file))
However, .abspath() doesn't do what I think it should. If I have a directory like this:
music
--1.mp3
--2.mp3
--folder
----a.mp3
----b.mp3
----c.mp3
----d.m4a
----e.m4a
and I run my code, I get this output:
C:\Users\User\Documents\python_music\1.mp3
C:\Users\User\Documents\python_music\2.mp3
C:\Users\User\Documents\python_music\a.mp3
C:\Users\User\Documents\python_music\b.mp3
C:\Users\User\Documents\python_music\c.mp3
C:\Users\User\Documents\python_music\d.m4a
C:\Users\User\Documents\python_music\e.m4a
I'm confused why it doesn't show the 5 files being inside of a folder.
Aside from that, am I even going about this in the easiest or best way? Again, I'm new to python so any help is appreciated.
You are passing just the filename to os.path.abspath(), which has no context but your current working directory.
Join the path with the subdir parameter:
print(os.path.join(subdir, file))
From the os.path.abspath() documentation:
On most platforms, this is equivalent to calling the function normpath() as follows: normpath(join(os.getcwd(), path)).
so if your current working directory is C:\Users\User\Documents\python_music all your files are joined relative to that.
But os.walk gives you the correct location to base filenames off instead; from the documentation:
For each directory in the tree rooted at directory top (including top itself), it yields a 3-tuple (dirpath, dirnames, filenames).
dirpath is a string, the path to the directory. [...] filenames is a list of the names of the non-directory files in dirpath. Note that the names in the lists contain no path components. To get a full path (which begins with top) to a file or directory in dirpath, do os.path.join(dirpath, name).
Emphasis mine.
I am trying to read some file which is inside some specific directory like.
I am trying to use python2.7+ version. Here is my requirements like:
Find directory starts with B1234 (here 1234 is nr) inside output folder
if directory exists then goto directories starting with only TEST_
read only file endswith YYYY.txt
but that can reside inside subdirectory (name is not important here)
I am trying to make following code working but in vain
for root, dirs, files in os.walk('output'):
for file in files:
if file.startswith('test') and file.endswith('_YYYY.txt'):
try:
f = open(os.path.abspath(os.path.join(root,file)) , 'r')
except:
print 'oops'
Problem is here I can find all desired files but also from unwanted directories..
i would like to use like that
for dir in dirList:
if dir startswith(B1234)
for testdir in os.listdir(dir)
if dir.startswiht(TEST_)
goto dir
search for file
Any help will be appreciable..please askif you need more info:
I think you need to make the change here :
for root, dirs, files in os.walk('output'):
for dir in dirs:
if dir.startswith('B1234') is False:
continue
for r, drs, fls in os.walk(dir):
# now write your code here