mardi 30 juin 2015

Using argparse, how can I put the user input into a list?

I need the file path and os.path.dirname does not give me the complete path (it does not include the file itself - e.g. home/a/b instead of home/a/b/filename). Also I need the file name so that I can print it later. Since the argument the user inputs IS the filename, I need a way to store the input into a list.

import sys
import argparse
import inspect, os
import os.path

file_list = []

if __name__ == '__main__':
    parser = argparse.ArgumentParser()      
    parser.add_argument('file', type=argparse.FileType('r'), nargs='*')
    args = parser.parse_args()

    for f in args.file:
        with f:
            data = f.read()
            print data
            x = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))
            print x
            file_list.append(x+#I need the filename here)

Aucun commentaire:

Enregistrer un commentaire