I'm trying the get from user a string and place it in the os.walk function.
This is my code:
def FiletypeNumber():
Path=Boaz.get()
Pathw="'"+Path+"'"
print (Pathw)
for (dirpath, dirnames, filenames) in walk(Pathw):
f.extend(filenames)
for i in range(len(f)):
t = f[i]
indexO=t.rindex('.')
LenF=len(t)
Ex=(t[-(LenF-indexO):])
FileTypeList.append(Ex)
if Ex in Typofiles:
pass
else:
Typofiles.append(Ex)
When I print the variable Pathw, I get the wanted results (for example: 'd:\js').
But when I pass this variable to the walk function, my code doesn't work properly.
Its purpose is to:
Enter a directory.
Print the number and type of files in it.
Don't add ' to your path name, such a path does not exist and results in a empty list.
def FiletypeNumber():
path = Boaz.get()
print('{!r}'.format(path))
for (dirpath, dirnames, filenames) in walk(path):
for filename in filenames:
_, ext = os.path.splitext(filename)
FileTypeList.append(ext)
if ext not in Typofiles:
Typofiles.append(ext)
Related
I wrote a script which will scan the shared folders and get the file age of all the files in those folders and some other file meta info.
my_paths = []
for p in ['\\\\data\\hold\\app1','\\\\data\\hold\\app2']:
for dirpath, dirnames, filenames in os.walk(p):
my_paths.append(filenames)
for filename in filenames:
full_path = os.path.join(dirpath, filename)
if last_modified(full_path) < threshold:
string_output='Age :' +age_file(full_path)+'\n'+'File Size : '+file_size(full_path)+'\n'
print(string_output)
#create(string_output);
I have to hard code all the shares in the code, instead I thought of making the script to read from a text file where I can update all the paths.
text.txt
\\data\hold\app1
\\data\hold\app2
revamp code,
my_paths = []
for line in open(r'd:\mydata\text.txt').readlines():
my_paths.append(line.strip())
print my_paths
for dirpath, dirnames, filenames in os.walk(line):
my_paths.append(filenames)
for filename in filenames:
full_path = os.path.join(dirpath, filename)
if last_modified(full_path) < threshold:
string_output='Age :' +age_file(full_path)+'\n'+'File Size : '+file_size(full_path)+'\n'
print(string_output)
#create(string_output);
my_paths returning the data like ['\\data\hold\app1','\\data\hold\app2'] but somehow the rest is not working.
You simply forgot to strip the line you read in the loop:
...
for dirpath, dirnames, filenames in os.walk(line.strip()):
...
Lets say the directories hierarchy looks like this:
A(root)
|
B---------C--------D
| | |
fileB.h fileC.png fileD.py
fileC1.jpg
E
|
fileE.py
How can I access all the doc? Or just get the path. Is there a way to iterlate all?
What I do:
path = sys.path[0]
for filename_dir in os.listdir(path):
filename, ext = os.path.splitext(filename_dir)
if ext == '.h':
#do something
elif ext == '.png'
#do something
.....
But as I know listdir can only access the directory where my program's py file located.
This gives only the dirs and files under a directory, but not recursively:
import os
for filename in os.listdir(path):
print filename
If you want to list absolute paths:
import os
def listdir_fullpath(d):
return [os.path.join(d, f) for f in os.listdir(d)]
If you want resursive search, this gives you an iterator that returns 3-tuples including the parent directory, list of directories, and list of files at each iteration:
for i,j,k in os.walk('.'):
print i, j, k
For example:
import os
path = sys.path[0]
for dirname, dirnames, filenames in os.walk(path):
for subdirname in dirnames:
print "FOUND DIRECTORY: ", os.path.join(dirname, subdirname)
for filename in filenames:
print "FOUND FILE: ", os.path.join(dirname, filename)
With this code, I am trying to read all files a directory and all its subdirectories. I have another list of files names, if the search finds files in the directories that are on the other list, I want to copy those feature classes to another location. When the code gets to FeatureClasstoGeodatabase, I keep getting an error that the input features data type is not supported or does not exist. I wasn't sure if I needed to somehow get the path as well as the filename so I created a couple of lists to capture that separately, but I'm kind of stuck here:
import arcpy
import os
workspace = r'F:\SF_HMP - transferred to Ydrive'
output_loc = r'C:\temp\temp.gdb'
mssng_files = r'F:\SF_HMP - transferred to Ydrive\Maps\broken_links_missing_files.txt'
files_to_find = []
layers_list = []
layers_path = []
with open(mssng_files) as filelist:
for line in filelist:
files_to_find.append(line.strip())
for dirpath, dirnames, filenames in arcpy.da.Walk(workspace,datatype="FeatureClass"):
for filename in filenames:
layers_list.append(filename)
layers_path.append(os.path.join(dirpath,filename))
for lyr in layers_list:
if lyr in files_to_find:
arcpy.FeatureClassToGeodatabase_conversion(lyr,output_loc)
I realized I needed to specify the workspace for each file to be copied over. I also repeated the code to search for and copy over rasters and tables:
import arcpy,os, easygui,sys
mssng_files = r'L:\SF_HMP - transferred to Ydrive\Maps\broken_links_missing_files.txt'
wkspc = easygui.enterbox("Enter workspace path:",title='Search for Files')
output_loc = easygui.enterbox("Output location:",title='Copy Files')
with open(mssng_files) as filelist:
for line in filelist:
files_to_find.append(line.strip())
for dirpath, dirnames, filenames in arcpy.da.Walk(wkspc,datatype='FeatureClass'):
for filename in filenames:
if filename in files_to_find:
ws_l = os.path.join(dirpath,filename)
arcpy.env.workspace = ws_l
arcpy.FeatureClassToGeodatabase_conversion(ws_l,output_loc)
for dirpath, dirnames, filenames in arcpy.da.Walk(wkspc,datatype='RasterDataset'):
for filename in filenames:
if filename in files_to_find:
ws_r = os.path.join(dirpath,filename)
arcpy.env.workspace = ws_r
arcpy.RasterToGeodatabase_conversion(ws_r,output_loc)
for dirpath, dirnames, filenames in arcpy.da.Walk(wkspc,datatype='Table'):
for filename in filenames:
if filename in files_to_find:
ws_t = os.path.join(dirpath,filename)
arcpy.env.workspace = ws_t
arcpy.TableToGeodatabase_conversion(ws_t,output_loc)
I want to return the path of a file, If it is found by the program, but I want it to continue to loop(or recursively repeat) the program until all files are checked.
def findAll(fname, path):
for item in os.listdir(path):
n = os.path.join(path, item)
try:
findAll(n, fname)
except:
if item == fname:
print(os.idontknow(item))
So I'm having trouble with calling the path, right now I have
os.idontknow(item)
as a place holder
Input is :
findAll('fileA.txt', 'testpath')
The output is:
['testpat\\fileA.txt', 'testpath\\folder1\\folder11\\fileA.txt','testpath\\folder2\\fileA.txt']
Per my comment above, here is an example that will start at the current directory and search through all sub-directories, looking for files matching fname:
import os
# path is your starting point - everything under it will be searched
path = os.getcwd()
fname = 'file1.txt'
my_files = []
# Start iterating, and anytime we see a file that matches fname,
# add to our list
for root, dirs, files in os.walk(path):
for name in files:
if name == fname:
# root here is the path to the file
my_files.append(os.path.join(root, name))
print my_files
Or as a function (more appropriate for your case :) ):
import os
def findAll(fname, start_dir=os.getcwd()):
my_files = []
for root, dirs, files in os.walk(start_dir):
for name in files:
if name == fname:
my_files.append(os.path.join(root, name))
return my_files
print findAll('file1.txt')
print findAll('file1.txt', '/some/other/starting/directory')
Something like this, maybe?
import os
path = "path/to/your/dir"
for (path, dirs, files) in os.walk(path):
print files
Functions return the position of master well in python.
def myPath():
for root,dirs,files in os.walk(dir):
for fn in files:
path = os.path.join(root, fn)
return path
return path
return path
ls /home/bb/C/
a.out main.c simple_write t.c
Three positions "return" is not what I want.
I want to get result "All the files in the C"
def filesize(path):
for root, dirs, files in os.walk(PATH):
for fn in files:
path = os.path.join(root, fn)
size = os.stat(path).st_size
yield size,path
for size,path in filesize(PATH):
print size,path
but,How to achieve the above functions with the following code? How to modify it?
def find(path):
return [os.path.join(root,fn)
for root,dir,files in os.walk(dirs)
for fn in files]
Return a list of the paths, not only a single path:
def find(path):
return [os.path.join(root, fn)
for root, dirs, files in os.walk(path)
for fn in files]
You could also use yield inside the inner loop to get a generator function, see The Python yield keyword explained.
What you want is a generator:
def myPath(mydir):
for root,dirs,files in os.walk(mydir):
for fn in files:
path = os.path.join(root, fn)
yield path # <----- Instead of return
for path in myPath(some_dir):
print path
Here are 3 ways if you need just the /home/bb/C directory listed
import os, glob
def listWithoutDirectories(path):
return [os.path.join(root,fn)
for root,dirs,files in os.walk(mydir)
if(root==path)
for fn in files]
def listWithDirectories(path)
return [os.path.join(path,fn)
for fn in os.listdir(path)]
print listWithoutDirectories('/home/bb/C')
print listWithDirectories('/home/bb/C')
#Or with a glob
print glob.glob('/home/bb/C/*')