not listing entire directory - python

Im new on Python, Im actually on a short course this week, but I have a very specific request and I dont know how to deal with it right now: I have many different txt files in a folder, when I use the following code I receive only the filename of two of the many files, why is this?
regards!
import dircache
lista = dircache.listdir('C:\FDF')
i = 0
check = len(lista[0])
temp = []
count = len(lista)
while count != 0:
if len(lista[i]) != check:
temp.append(lista[i- 1])
check = len(lista[i])
else:
i = i + 1
count = count - 1
print (temp)

Maybe you can use the glob library: http://docs.python.org/2/library/glob.html
It seems that it works UNIX-like for listing files so maybe it can work with this?
import glob
directory = 'yourdirectory/'
filelist = glob.glob(directory+'*.txt')

If I've understood you correct, you would like to get all files?
Try it in this case:
import os
filesList = None
dir = 'C:\FDF'
for root, dirs, files in os.walk(dir):
filesList = files
break
print(filesList)
If need full path use:
import os.path
filesList = None
dir = 'C:\FDF'
for root, dirs, files in os.walk(dir):
for file in files:
filesList.append(os.path.join(root, file))
print(filesList)

Related

Is there an efficient way to recurse in a directory?

I want to perform:
iterate over the content of the folder
if content is file, append to list
if content is folder, goto 1
if folder name is "depth" or "ir", ignore
I am using python. Can you help?
ended up doing something like:
_files = []
dir = "path/to/folder"
for root, dirs, files in os.walk(dir, topdown=False):
for name in files:
files = os.path.join(root, name)
if root.split("/")[-1] in ["depth", "ir"]:
continue
_files.append(files)
print(_files)
The os.walk() will recurse for you.
import os
res = []
for (root, dirs, files) in os.walk('/path/to/dir'):
# root is the absolute path to the dir, so check if the last part is depth or ir
if root.split("/")[-1] in ["depth", "ir"]:
continue
else:
# files is a list of files
res.extend(files)
print(res)
Try this
import os
basepath ="<path>"
files=[]
def loopover(path):
contents = os.listdir(path)
for c in contents:
d = os.path.join(path,c)
if os.path.isfile(d):
files.append(c)
if os.path.isdir(d):
if (c=="depth" or c=="ir"):
continue
else:
loopover(d)
loopover(basepath)

How can I accurately search a directory with elements from a text file?

Fairly newish to python. I'm trying to use a list made from a text file to search directories for files and folders. Its working to certain extent but only if the search element is at the start of the file/folder name as I'm using .startswith().
The code I'm using is below.
I'm thinking regex might be the way to go but can't seem to figure it out.
Any help appreciated.
Thanks.
import os
def data_cleansing(path):
dirscount = 0
filescount = 0
with open("Y:\Admin\Data Cleansing\DCList.txt","r") as f:
x = f.readlines()
x = [x.replace("\n","") for x in x]
#print(x)
for root, dirs, files in os.walk(path):
for dirs in dirs:
if dirs.startswith(tuple(x)):
dirscount = dirscount + 1
#print(dirscount)
print(os.path.join(dirs))
for root, dirs, files in os.walk(path):
for files in files:
if files.startswith(tuple(x)):
filescount = filescount + 1
#print(filescount)
print(os.path.join(files))
total = (dirscount + filescount)
print(total,"files and folders found in",path,"that need cleansing.")
data_cleansing(r"G:\Processed\failed")
print("*"*70)
data_cleansing(r"G:\Processed\done")
print("*"*70)
data_cleansing(r"S:\Prosort")
print("*"*70)
data_cleansing(r"S:\Prosort_Archive")
Does this fit your needs?
from os import scandir
find_list = ['Uni', '.doc', 'XML']
path = r"c:\\Autoit\\ForumTests\\"
def scantree(path):
"""Recursively yield DirEntry objects for given directory."""
for entry in scandir(path):
if entry.is_dir(follow_symlinks=False):
yield from scantree(entry.path)
else:
yield entry
if __name__ == '__main__':
for entry in scantree(path):
for pattern in find_list:
if pattern in entry.path:
print(pattern, '-->', entry.path)

Walk through folders until specific ending number python

I have a range of folders which are named like folder0, folder2,..., folder99. Now I want to walk through folder0,..., folderX and print their files. X should stay variable and easy to change.
My code looks something like this but its not working how I want it to work yet because I can't decide until which number I want to go.
import os
import re
rootdir = r'path'
for root, dirs, files in os.walk(rootdir):
for dir in dirs:
if not re.match(r'folder[0-9]+$', dir):
dirs.remove(dir)
for file in files:
print files
Assuming your name scheme is consistent, which you state, why do the os.walk?
import os
dir_path = '/path/to/folders/folder{}'
x = 10
for i in range(0, x):
formatted_path = dir_path.format(i)
try:
for f in os.listdir(formatted_path):
filename = os.path.join(formatted_path, f)
if os.path.isfile(filename):
print filename
except OSError:
print "{} does not exist".format(formatted_path)

How to find .asc files 5 folders deep?

I have several .asc files hidden 5 folders deep. For example:
main > Folder1a > Folder2a > Folder3a > Folder4a > myfile1.asc
main > Folder1b > Folder2b > Folder3b > Folder4b > myfile2.asc
main > Folder1c > Folder2c > Folder3c > Folder4c > myfile3.asc
What method can I use to get a list of myfile.asc files contained within the main folder?
f_walker = os.walk("/path/to/main")
f_generator = (os.path.join(cur,file) for cur,f,d in f_walker for file in f)
all_files = [file for file in f_generator if file.endswith(".asc")]
I think ... but it might go slow if you know they will only ever be 5 levels deep (never 4 and never 6... then you can optimize this some)
for example something like this might be much faster
import os
import glob
def go_deep(base_path,pattern,depth):
if depth > 0:
for fname in os.listdir(base_path):
full_path = os.path.join(base_path,fname)
if os.path.isdir(full_path):
for fpath in go_deep(full_path,pattern,depth-1):
yield fpath
else:
for fname in glob.glob(pattern):
yield os.path.join(base_path,fname)
print list(go_deep(".",pattern="*.asc",depth=5))
This will work. Give the extension with the dot. Directory does not need quotes. Basically same as Joran's answer with input from user. Did this for another project...
import os
extension = input("enter extension:")
directory = input("enter directory to start in:")
for root, dirs, files in os.walk(directory):
for fname in files:
if fname.endswith(extension):
full_fname = os.path.join(root, fname)
print(full_fname)

Error on deleting file in a python code

I have a little code that allows me to print images arriving in a folder.
But I'd like them to be deleted just after having been printed - I want to try this on a RaspberryPI and I will not have enough space to store all images.
If someone can help me it would be very much appreciated.
Here is my code:
monRep = "/Users/XX/Desktop/XXX/"
import os, mimetypes, random
while True:
fpaths = []
for fname in os.listdir(monRep):
fpath = os.path.join(monRep, fname)
if os.path.isfile(fpath):
mt = mimetypes.guess_type(fpath)[0]
ext = os.path.splitext(fpath)[1]
if mt: mt = mt.split('/')[0].lower()
else: mt = False
#if ext.lower() in ('.bmp','.pict', '.JPG', '.jpg', '.pdf'): mt = 'image'
if mt in ('image',): fpaths.append(fpath)
for fpath in fpaths:
newpath = fpath.replace('/Users/XX/Desktop/XXX/','/Users/XX/Desktop/XXX2/')
os.rename(fpath,newpath)
command = "lpr "+newpath
print (command)
os.system(command)
I tried to write at the end
os.remove ('/Users/Aym/Desktop/eden2/')
But then I had this :
SError: [Errno 1] Operation not permitted: '/Users/XX/Desktop/XXX2/'
I tried the shutil method recommanded on this forum
import os
import shutil
for root, dirs, files in os.walk('/Users/XX/Desktop/XXX2/'):
for f in files:
os.unlink(os.path.join(root, f))
for d in dirs:
shutil.rmtree(os.path.join(root, d))
but nothing happened
I have also less space on a Raspberry. What you can exchange first of all is:
LIST_NAME = ['.jpg', '.bmp', '.mickey_mouse']
file = [file for file in os.listdir(CAMERA_DIR) for type_name in LIST_NAME if file.endswith(type_name)]
That is somehow equivalent to your whole search engine above.
where LIST_NAME is whatever you look for and CAMERA_DIR == "/Users/XX/Desktop/XXX/"
The lines above will make your code easier and compact without usage of extra modules.
You can use a simple move action from XX to XX2 (shutil module) for each found file under os.path.join(CAMERA_DIR, file_name).
Last step after you print or do whatever, remove completely the XX2 folder with simple command:
shutil.rmtree(XX2)
This is definitely work and clean up your code in Python3. Have fun!
What I can even recommend is: usage of /tmp/NAMEXX folder. In worst case it is only on the temporary folder of your raspberry!

Categories