0

I need to explain more clearly because from the title you can't completely comprehend my needs...

    File = input ("Insert the name of the file: ")

    IV = np.genfromtxt('D:\User\Python\Programms_Python\programms\%d.txt', 
    File, usecols=(1,5,2,5))

I need to insert from command line the name of the file I want to analyze, because I have to do multiple analyses on many different .txt files and inserting their names directly from the command line would make my job much faster. Can you suggest a way to insert it inside the function genfromtxt? Is it possible?

genfromtxt is a function inside numpy and it allows you to copy the values of a column in your txt inside an array, using usecols you can ask which column to copy in the array which this way can become a multidimensional array, like in this case IV is a four dimensional array.

Trying the way I showed above, genfromtxt doesn't recognize the %d -> File (the names of the .txt files are just numbers and I tried also with %s but it didn't change) so please.. HELP

Ateris
  • 1
  • 1
  • possible duplicate of [Command Line Arguments In Python](http://stackoverflow.com/questions/1009860/command-line-arguments-in-python) – kdopen May 05 '15 at 18:20
  • You may want to investigate the `glob()` function within the module of the same name (`glob`). Here is a link to the documentation: https://docs.python.org/2/library/glob.html#glob.glob – ArtOfWarfare May 06 '15 at 15:09

1 Answers1

1

Probably the easiest way of doing this would be the get the command line arguments via sys.argv, then to loop over them, passing them one at a time to your function genfromtext (I have no idea what genfromtext is, by the way. Your question makes no mention of where it comes from. But from your usage, I assume it's a function in a 3rd party module np which you also told us nothing about.) So, anyways, your code would look something like this:

from sys import argv

for arg in argv[1:]:
    IV = np.genfromtext(arg, usecols=(1,5,2,5))

The [1:] after argv is so you don't get the first command line argument, which would just be the name of your Python script file.

ArtOfWarfare
  • 20,617
  • 19
  • 137
  • 193
  • sorry dude, I'm new on python so I forgot to add some details :D genfromtxt is a function in numpy and it open a file and copy in a vector the columns you declare in usecols. though what i need is to add a string of characters (the name of the file that I want to pass to my program) inside the string that contain the adresse to the folder with my files in it... I don't know if this way is clearer. sorry thouhg for not being clearer. and sorry for my bad english! – Ateris May 06 '15 at 13:05