0

I want to:

As the title says,

run something such as:

python3 program.py [~/home/me/Documents/*.txt]

and I would get

['text1.txt', 'text2.txt', ... , 'textn.txt']

Solutions I found online:

A lot of solutions I found online included using fileinput.input() and sys.argv. The problem is that I am not correctly implementing those solutions (I am a complete noob at Python).

Here's the most promising solution:

Source

import fileinput
for line in fileinput.input():
    process(line)

but it keeps telling me that "process is not defined" - - I have yet to find a solution.

Another solution is sys.argv[0]. but it keeps telling me that the list index is out of range.

EDIT: This solution doesn't go over command line argument for python. This is what I'm mostly having trouble with.

Community
  • 1
  • 1
Adam
  • 3
  • 1
  • 2
  • 5
  • Possible duplicate of [How to list all files of a directory in Python](http://stackoverflow.com/questions/3207219/how-to-list-all-files-of-a-directory-in-python) – barak manos Jan 30 '17 at 18:17
  • 1
    @barakmanos Hello Barak, this solution doesn't go over directories as command line arguments unfortunately. That is what I'm mostly having trouble with. – Adam Jan 30 '17 at 18:25
  • You're getting the error literally because `process` is not defined. Maybe `print(line)` would do the trick for you here. – spicypumpkin Jan 30 '17 at 18:31
  • OK, sorry. I will revoke the 'duplicate-vote', but will keep that comment above for reference, because it does partially answer the question IMO. – barak manos Jan 30 '17 at 18:33
  • Hi @Posh_Pumpkin, print(line) only prints the argument, but doesn't print a list of the .txt files in the directory, which is what I need. – Adam Jan 30 '17 at 18:41

2 Answers2

0

This works for me:

import os
import sys

def getfilenames(path):
    ftype = path.split(".")[-1]
    folder = path.split("*")[0]
    return [i for i in os.listdir(folder) if ftype in i.split(".")[-1]]

print(getfilenames(sys.argv[1]))

Supply the path as a string:

python example.py "/path/to/file/*.type" 
Tristan
  • 1,576
  • 9
  • 12
  • 1
    Hi @Tristan, I keep getting "list index out of range" error for "print(getfilenames(sys.argv[1]))" Would I accommodate the integer in argv[] to satisfy the number of files in the directory? – Adam Jan 30 '17 at 18:44
  • Hi @Adam. The list argv holds the list of command line arguments passed to a python script. argv[0] is the script name, while the path should be argv[1]. Did you supply the path as an argument? EDIT: argv[1] is only present if you execute the script over the command line, like in my example. If you do not want to run it over the command line call getfilenames("/path/*.txt") – Tristan Jan 30 '17 at 18:47
  • Unfortunately, as soon as I run it it gives me the "list index out of range" error. So if I want to do "python3 program.py ~/home/name/*.txt " this wouldn't work? – Adam Jan 30 '17 at 19:06
  • It should work if you supply the directory as a string "/home/name/*.txt". Have you tried that? – Tristan Jan 30 '17 at 19:15
0

you can combine the answers presented in how to list all files of a directory and command line arguments in python to create something like this:

import sys
from os import walk

path = sys.argv[1]
files = []
for (dirpath, dirnames, filenames) in walk(path):
    files.extend(filenames)
print (files)

and run it with

python3 listdir.py $PWD
Community
  • 1
  • 1
Asad
  • 5
  • 3
  • Hi @Asad, unfortunately, it strictly has to be "python3 listdir.py ~/name/file/file2/*.txt" format – Adam Jan 30 '17 at 18:41
  • updated the code snippet to be python3 compatible. let me know if this helps. ah nvm, i think I am missing the type qualifier. will fix – Asad Jan 30 '17 at 18:48