Open every file that has extension .ab?

This is the place for queries that don't fit in any of the other categories.

Open every file that has extension .ab?

Postby adam » Mon May 26, 2014 12:48 am

Sorry I'm very noob, I have a script that I use to write a couple of bytes into a file to make it readable by a different type of machine than it was intended for.
The script looks something like this:
Code: Select all
import sys
for file in sys.argv[1:]:
   f = open(file, 'r+b')
   print "wrote " + file

So as you can see from this I have to list every file that I want to modify as an argument on the command line. For instance; file1.ab file2.ab file3.ab
My question is, is there someway I can modify this script so that it will open every file in a directory that has an extension .ab? Something like *.ab

Please be gentle with any suggestions, I'm not a coder...
Last edited by Yoriz on Mon May 26, 2014 1:18 am, edited 1 time in total.
Reason: First post lock, Added code tags.
Posts: 2
Joined: Mon May 26, 2014 12:29 am

Re: Open every file that has extension .ab?

Postby Yoriz » Mon May 26, 2014 1:24 am

Hi welcome to the forum.
Please read the new user read this link in my signature.

Take a look at
New Users, Read This
Join the #python-forum IRC channel on!
User avatar
Posts: 1247
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

Re: Open every file that has extension .ab?

Postby Larz60+ » Mon May 26, 2014 3:56 am


The code below is designed to list the contents of all zip files in a given directory. It uses the tkinter filedialog to get the directory name, then opens each file and lists the filenames that are inside of the zips. You can use the os.listdir to save a list of all files, then use the same method I use for the zipfiles to select your .ab files, just use a regular open. You can peel off the gui stuff if you don't need it.

Hope this helps


Code: Select all
from tkinter import *
import tkinter.filedialog
import os
import zipfile

class Application(Frame):
    def setDirectory(self):
        """Returns a selected directoryname."""
        dir_opt = options = {}
        options['initialdir'] = '/mnt/OneDown/Patent1/USpatents/data/'
        options['mustexist'] = False
        options['parent'] = root
        options['title'] = 'Set Directory'
        return tkinter.filedialog.askdirectory(**dir_opt)
    def zipList(self):
        zipDir = self.setDirectory()
        zipDirfiles = os.listdir(zipDir)
        for zipFilename in zipDirfiles:
            if '.zip' in zipFilename:
                print("%s \n" % zipFilename)
                with zipfile.ZipFile ( zipFilename, "r") as zfile:
                    for name in zfile.namelist():
                        print("  %s\n" % name)
    def createWidgets(self):
        self.QUIT = Button(self)
        self.QUIT["text"] = "Exit"
        self.QUIT["fg"]   = "red"
        self.QUIT["command"] =  self.quit

        self.QUIT.pack({"side": "left"})

        self.hi_there = Button(self)
        self.hi_there["text"] = "List Zip Files",
        self.hi_there["command"] = self.zipList

        self.hi_there.pack({"side": "left"})

    def __init__(self, master=None):
        Frame.__init__(self, master)

def MainProc():
    global root
    root = Tk()
    app = Application(master=root)

Posts: 368
Joined: Thu Apr 03, 2014 4:06 pm

Re: Open every file that has extension .ab?

Postby stranac » Mon May 26, 2014 7:45 am

If you're using a decent shell, it should expand *.ab so all the .ab files get passed to your script.
Or you could do the same thing using the glob module, as suggested by Yoriz.
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
Posts: 1284
Joined: Thu Feb 07, 2013 3:42 pm

Re: Open every file that has extension .ab?

Postby adam » Mon May 26, 2014 9:15 am

Thanks for all the suggestions so far, after studying them it seems it is all still way over my head. I guess since I have no formal programing training (and am an old man by internet standards) my lack of knowledge of the basic fundimentals is probably letting me down.
Larz's sample code with the gui stuff in it opened my eyes, I didnt even know python could do a gui open dialog type thing so it seems I have a lot to learn.
Yoriz - thanks, that glob module seems to be the most direct route to achieve what I asked for, and I will read the liink page another 20 times to see if I can fumble something together.
Stanac - now sorry I'm going to sound very thick saying this and it probably shows how little I understand about this stuff, but what do you mean by "a decent shell"? is this my OS you are talking about or is there some other front end for python? Before my original posting here I did already try every type of 'wildcard'.ab string that I knew and couldnt get it to work so I guess that means I dont have a decent shell!

Anyway, thanks all for taking the time to post. I will keep playing around with this stuff and see where it gets me.
Posts: 2
Joined: Mon May 26, 2014 12:29 am

Return to General Coding Help

Who is online

Users browsing this forum: dhee2020, Google [Bot], malenoir and 3 guests