I'm trying to write a slideshow program, and part of that is adding image files. To do this, I want to be able to search a selected folder for all files that have certain extensions. I have looked, and I believe what I have below should work to pull in certain files. However, in testing I find that it is pulling in every file, and not just ones with the proper extension. Any help in figuring this out would be amazing, thank you!
def image_load_test(image_loop_path):
image_file_table_length = 0
path = image_loop_path
image_file_table = []
valid_images = [".gif",".png",".tga"]
for f in os.listdir(path):
ext = os.path.splitext(f)[1]
if ext.lower() not in valid_images:
continue
image_file_table.append(os.path.join(path,f))
image_file_table_length = image_file_table_length + 1
return image_file_table
I can tell this isn't working because I have a folder with many .png and .jpg images, and even though I am not adding .jpg to the valid_images table, those files still end up listed.
#Nathan, the following code can help you identifying the type of files
import glob
path_file = r'C:/Users/...' #path/folder of files where all the png files are kept
types = ('*.jpg', '*.png') #this is where you add the required file type
all_files = []
for files in types:
all_files.extend(glob.glob(path_file + files))
Take out the 'not' from the if statement line, remove your 'continue' you don't need it, and put the next two lines where it is instead (tab them in).
This ended up working for me, thank you everyone!
def image_load_test(image_loop_path):
image_file_table_length = 0
path = image_loop_path
image_file_table = []
valid_images = [".jpg",".png",".tga",".jpeg"]
for f in os.listdir(path):
ext = os.path.splitext(f)[1]
if ext.lower() in valid_images:
image_file_table.append(os.path.join(path,f))
image_file_table_length = image_file_table_length + 1
continue
else:
continue
return image_file_table
Related
I am working on a project where I need to sort .jpg files and folders that contain .jpg files. I have other scripts that are functional which I intend to incorporate into this python script later. First though, I've implemented in the first script below to count the number of underscores in a file and take action based on the result and this works successfully. I need help on creating logic that will go through .jpg image files and if the files have more than one underscore the program will move the files into an error folder. Also any feedback on how to optimize this script would be greatly appreciated!
from pathlib import Path
import shutil, os, time, glob
timestr = time.strftime("%Y%m%d-%H%M%S")
folder = 'D:\\test\\testing'
working_folder = 'DitaTest1'
full_path = Path(os.path.join(folder, working_folder))
test_path = folder + '\\' + working_folder
for file_path in full_path.iterdir():
file_name = file_path.name
result = file_name.count('_')
if file_path.is_file():
os.chdir(test_path)
for file in glob.glob("*.jpg"):
dst=test_path+"\\"+file.replace(" ","_").replace(".jpg","") # .replace("Angle","").replace("Front","").replace("Side","")
os.mkdir(dst)
# print(dst)
shutil.move(file,dst)
elif result != 1:
if not file_path.is_file():
shutil.move(os.path.join(folder, working_folder, file_name), os.path.join(folder, working_folder + ' - dir-ERRORS_' + timestr, file_name))
else:
print('Ignored operation')
You need to explain more so that we can understand it better but from what I have read,
Your if logic seems to be wrong, if you want to check the number of underscores you shouldn't put that logic in elif. You should try sth like this instead.
for file_path in full_path.iterdir():
file_name = file_path.name
result = file_name.count('_')
if os.path.isdir(file_path):
pass
else:
if result == 1:
os.chdir(test_path)
for file in glob.glob("*.jpg"):
dst=test_path+"\\"+file.replace(" ","_").replace(".jpg","") # .replace("Angle","").replace("Front","").replace("Side","")
os.mkdir(dst)
# print(dst)
shutil.move(file,dst)
else:
shutil.move(os.path.join(folder, working_folder, file_name), os.path.join(folder, working_folder + ' - dir-ERRORS_' + timestr, file_name))
What this code does is, iterate over the folder and if it finds a folder it will just pass and when it finds a file it will check if result == 1. If it is it will move it to your desired folder, otherwise it will move it to the error folder. If I made a mistake let me know.
I have a folder, which has many sub folders, and in these sub folders is json.gz file with the same name.And these json files are with the same structure.
Such as ...\a\0\b.json.gz
...\a\1\b.josn.gz
...\a\2\b.josn.gz
What I want to do is give the path to .../a then alter a certain value in the json file.
My code is like this:
def getAllSourceFile(folder):
arrSource = []
for root,dirs,files in os.walk(folder):
for file in files:
if file.__str__()=='b.json.gz':
allSourceFile = os.path.join(root,file)
arrSource.append(allSourceFile)
return arrSource
def ModRootFile(rootM_file,Scale):
f=gzip.open(rootM_file,'r')
file_content=f.read()
f.close()
d=file_content.decode('utf8').split(',')
for i in d:
doc=d.get(i)
jsondoc=json.dumps(doc)
jsondict=json.loads(jsondoc)
print(i)
for k in jsondict["2"]:
k["atlas"]="false"
d.save(jsondict)
I'm trying to change "atlas" to "false", it finished without error, or at least without error hint.but nothing changed.
Could some one please tell me what is wrong? Thank you.
The first part of my project is to create a loop in which it reads an unknown amount of text files. I am confused how to approach this as my past projects I have entered the entry of a file specifically e.g.
for line in open('text1.txt')
How do I make it so if there's e.g. 10 files generated while it checks my code my code will actually read through 10 files? I was thinking of
for line in range(0, _input_ + 1_
for line in open ???
But I have no luck figuring out what to do. Help would be highly appreciated, thanks :D
You can use something like this where ROOTDIR is a parent directory of your files:
# path to parent folder of your files
ROOTDIR = '/home/your_name/parent_folder'
for subdir, dirs, files in os.walk(ROOTDIR):
name = str(subdir).split('/')[-1]
print(subdir)
for f in files:
print('Working on file:', f)
SOURCE = str(subdir) + '/' + str(f)
# load text
text = open(SOURCE, encoding='utf-8').readlines()
I am doing a small project using a book about Python. One of the examples they show is how to rename all of the files and directories right underneath the root folder, however I want to go a step further and change files within sub directories.
I figured using os.walk would be my best bet but I can only get it to change file names not subdirectories. I would like to also change subdirectory names as I go through os.walk.
I am using a book and online resources to accomplish this, any help would be awesome.
Here is what I have right now based on the book. I have tried to include os.listdir but the regex won't work like that.
import shutil, os, re
#regex that matches files with dates
wrongFormat = re.compile(r"""^(.*?)
((0|1)?\d).
((0|1|2|3)?\d).
((19|20)\d\d)
(.*?)$
""", re.VERBOSE)
#Loop over the files in the working directory
path = '.'
for path, dirs, files in os.walk(path, topdown=True):
for incorrectDt in files:
dt = wrongFormat.search(incorrectDt)
#Skip file without a date
if dt == None:
continue
#Split filename into parts
beforePart = dt.group(1)
monthPart = dt.group(2)
dayPart = dt.group(4)
yearPart = dt.group(6)
afterPart = dt.group(8)
#Form the new date format
correctFormat = beforePart + monthPart + "_" + dayPart + "_" + yearPart + afterPart
#Get full, absolute file paths.
absWorkingDir = os.path.abspath(path)
incorrectDt= os.path.join(absWorkingDir, incorrectDt)
correctFormat = os.path.join(absWorkingDir, correctFormat)
#rename files
print ('Renaming "%s" to "%s"...' % (wrongFormat, correctFormat))
shutil.move(incorrectDt, correctFormat)
Im trying to create a single file out of multiple text files I have across multiple folders. This is my code for concatenating. It works only if the program file is placed in each folder:
import os
file_list = [each for each in cur_folder if each.endswith(".txt")]
print file_list
align_file = open("all_the_files.txt","w")
seq_list = []
for each_file in file_list:
f_o = open(file_path,"r")
seq = (f_o.read().replace("\n",""))
lnth = len(seq)
wholeseq = ">"+each_file+" | "+str(lnth)+" nt\n"+seq+"\n"
align_file.write(wholeseq)
print "done"
Now I tried to edit to make sure that it automatically runs through the entire Data folder and then enters the subdirectories and concatenates all the files without me having to paste the program file in each folder. This is the edit.
import os
dir_folder = os.listdir("C:\Users\GAMER\Desktop\Data")
for each in dir_folder:
cur_folder = os.listdir("C:\\Users\\GAMER\\Desktop\\Data\\"+each)
file_list = []
file_list = [each for each in cur_folder if each.endswith(".txt")]
print file_list
align_file = open("all_the_files.txt","w")
seq_list = []
for each_file in file_list:
f_o = open(file_path,"r")
seq = (f_o.read().replace("\n",""))
lnth = len(seq)
wholeseq = ">"+each_file+" | "+str(lnth)+" nt\n"+seq+"\n"
align_file.write(wholeseq)
print "done" , cur_folder
However when I run this , I get an error on the first file of the folder saying no such file exists. I can seem to understand why, specifically since it names the file which is not "hardcoded". Any help will be appreciated.
If the code looks ugly to you feel free to suggested better ways to do it.
Jamie is correct - os.walk is most likely the function you need.
An example based on your use case:
for root, dirs, files in os.walk(r"C:\Users\GAMER\Desktop\Data"):
for f in files:
if f.endswith('.txt'):
print(f)
This will print the name of every single file within every folder within the root directory passed in os.walk, as long as the filename ends in .txt.
Python's documentation is here: https://docs.python.org/2/library/os.html#os.walk