Python file extension

If the directory contains the ".m" and ".xml" files, I want the script to find both of them (which it will not do at the moment, instead it goes to the "else" statement). This argument should search for all files in the directory.

python script.py --dir C:\\path\\path\\*.* #This should take all files (doesn't matter what type ex 'm', 'xml' 'txt' etc.).

If the user only needs XML files, he writes * .xml and vice versa for the ".m" files. Please note that if the user wants only "XML" or "m" files, then the script will find it

def main(argv):
    args = argumentParser(argv)
    if args.dirname.endswith('.m'):
        overrideM(args)
    elif args.dirname.endswith('.xml'):
        xmlOverride(args)
    elif args.dirname.endswith(('.m', '.xml')): #Can I do like this?
        #Here I want to run both of my function.
        overrideM()
        xmlOverride()
    else:
        print "Error can't find files"

My 'function' (a small part of it)

def overrideM(args):
    for fileName in glob.glob(args.dirname):
        print fileName
        with open(fileName, 'r') as searchFile:
            my_files_content = searchFile.read()
        #...rest of my code

My XML function (a small part of it)

def xmlOverride(args):
    for fileName in glob.glob(args.dirname):
        print fileName
        with open(fileName, 'r') as searchFile:
            my_files_content = searchFile.read()
        #...rest of my code
+4
source share
3 answers

elif args.dirname.endswith(('.m', '.xml')) , args - , , , , , - :

def main(argv):
    # make argumentParser return a tuple
    args = argumentParser(argv)
    if sorted(args) == ('.m', '.xml'):
        overrideM()
        xmlOverride()

, , :

def main(argv):
    args = argumentParser(argv)
    for ext in args:
        generic_search(ext)

args.dirname.endswith(('.m', '.xml')) , , .m, .xml. , str.endswith , os.listdir, .

- - :

from argparse import ArgumentParser
import os

parser = ArgumentParser()
parser.add_argument("path")
parser.add_argument('ext', nargs='*')

args = parser.parse_args()
path = args.path
exts = args.ext

# what your glob is doing
for f in os.listdir(path):
    if f.endswith(tuple(exts)):
        with open(os.path.join(path, f)) as fle:
            print(fle.name)
            # do whatever

, , - , endswith .

glob, , :

from argparse import ArgumentParser
import os
from glob import iglob

parser = ArgumentParser()
parser.add_argument("path")
parser.add_argument('ext', nargs='*')

args = parser.parse_args()
path = args.path
exts = args.ext

for f in chain.from_iterable([iglob(path+"/*"), iglob(path+"/**/*")]):
    if f.endswith(tuple(exts)):
        with open(os.path.join(path, f)) as fle:
            print(fle.name)

, , . glob , , , , listdir filer endswith.

, dict :

from argparse import ArgumentParser
import os
from glob import iglob

def xml(f):
    print(f)

def m(f):
    print(f)

def text(f):
   print(f)

mapped = {"m":m, "xml":xml, "text":text}

parser = ArgumentParser()
parser.add_argument("path")
parser.add_argument('ext', nargs='*')

args = parser.parse_args()
path = args.path
exts = args.ext


for f in chain.from_iterable([iglob(path + "/*"), iglob(path + "/**/*")]):
    ext = f.rsplit(".", 1)
    if len(ext) == 2 and ext[1] in mapped:
        mapped[ext[1]](f)

- O (1), , , .

:

 $ python 3_payg.py  /home/padraic  .xml 
/home/padraic/sitemap.xml
/home/padraic/yacy/build.xml
/home/padraic/graphviz-master/graphviz.appdata.xml
+1

1) MS Windows, UNIX, , , . UNIX :

 python script.py $(ls home/) 

2) , args , : "file.xml, file.m, file.txt" , , .txt. , , .

, .

files = args.split(",")
for file in files:
     main(file)
0

, Bash, * , . sys.argv . Windows .

, ( argparse, ).

myscript.py c:\a\directory

glob iglob, .

import sys
import glob
import os
import itertools

# retreive your directory to expore, from the arguments
# just for the example, better use argparse
dir = sys.argv[1]

# At this point, you might wish to use os.path.abspath & friends
# to have a normalized directory, and check its existence with
# os.path.exists.

# The patterns to search for:
patterns = "*.xml", "*.m"

# a generator expression yielding things like 
# "c:\\my\\dir\\*.xml" and "c:\\my\\dir\\*.m"
joint_patterns  = (os.path.join(dir, pt) for pt in patterns)

# Glob the joint patterns into a super-generator:
files = itertools.chain.from_iterable( glob.iglob(pt) for pt in joint_patterns )

# Show the result. the '*' is there to evaluate the super generator
# or else it'd print something like 
# "<itertools.chain object at 0x7fd92ac9efd0>"
print(*files)

, .

You do not need to create a supergenerator chainin your case. You simply return a global iterator for each template:

import sys
import glob
import os
import itertools

# retreive your directory to expore, from the arguments
# just for the example, better use argparse
dir = sys.argv[1]
# The patterns to search for:
patterns = "*.xml", "*.m"

# a LIST  holding things like 
# "c:\\my\\dir\\*.xml" and "c:\\my\\dir\\*.m"
joint_patterns = [os.path.join(dir, pt) for pt in patterns]

# Glob iterators.
globs = [glob.iglob(pt) for pt in joint_patterns]

for xml_file_path in globs[0]:
    do_xml_stuff(xml_file_path)

for m_file_path in globs[1]:
    do_m_stuff(m_file_path)
0
source

All Articles