Python Forum
Can you please explain what the part after import sys is doing?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Can you please explain what the part after import sys is doing?
#1
Actually, I'm only trying to use the Python wc module to count words in a text file.

wc('/proc/cpuinfo') works nicely in Idle!

But I found this and I am trying to understand it!

I got this from https://code-maven.com/python-wc

It is about using wc (not the toilet Big Grin )

We have the function:

def wc(*filenames):
    results = {}
    for filename in filenames:
        chars = 0
        words = 0
        lines = 0
        try:
            with open(filename) as fh:
                for line in fh:
                    lines += 1
                    words += len(line.split())
                    chars += len(line)
            results[filename] = {
                'lines': lines,
                'words': words,
                'chars': chars,
            }
        except Exception as err:
            print(err)
    return results
Then the main part, which I can't follow. Can you please explain it to me??

if __name__ == '__main__':
    import sys
    if len(sys.argv) < 1:
        exit("Usage: {} FILENAMEs".format(sys.argv[0]))
    results = wc(*sys.argv[1:])
    totals = {
        'lines': 0,
        'words': 0,
        'chars': 0,
    }
    for filename in results:
        res = results[filename]
        print("{} {} {} {}".format(res['lines'], res['words'], res['chars'], filename))
        for k in res:
            totals[k] += res[k]
    print("{} {} {} {}".format(totals['lines'], totals['words'], totals['chars'], 'total'))
How this main part supposed to know where the files are? Which files?

I made the above code, both parts, into a function, myApp() If I run myApp() in Idle I get:

Quote:>>> myApp()

0 0 0 total
>>>

My path is:

path = '/home/pedro/winter2020/20PY/dumpFiles/'

I thought, if I just pass it a list, the "splat" would open the list and the function would do the job, but I get an error.

>
Quote:>> myresults = wc(pathtoFiles)

expected str, bytes or os.PathLike object, not list

I got the files:

files = os.listdir(path)
Then ran this to get a list of path + file:

for file in files:      
    getFile = os.path.join(path, file)    
    if os.path.isdir(getFile): # this checks if file is a folder
        continue # don't get folders, use continue
    else:
        pathtoFiles.append(getFile)
I have a nice list of path + filename, but the function wc() above doesn't like this. Why is that?
Reply
#2
These are some of the crucial lines...

    if len(sys.argv) < 1:
        exit("Usage: {} FILENAMEs".format(sys.argv[0]))
This is assuming that it's running as a command-line program, not just a function. It's looking at sys.argv, which has the arguments the program was run with. If you don't give it any (sys.argv has only the python program), then it exits with a usage message.

    results = wc(*sys.argv[1:])
It takes all the passed in arguments and passes those on to wc. So if you call your script from the command shell like:

C:\> myscript file1 file2
Then wc is called with the argument list ["file1", "file2"].
Reply
#3
Thanks!

How can I pass my list pathtoFiles to it?

I was hoping
def wc(*filenames):

would splat open the list and process it. But no!

pathtoFiles looks like this:

['/home/pedro/winter2020/20PY/dumpFiles/shikai_capital_letters.txt', '/home/pedro/winter2020/20PY/dumpFiles/Youjie_info.txt', '/home/pedro/winter2020/20PY/dumpFiles/info1_3.txt', '/home/pedro/winter2020/20PY/dumpFiles/capital_letters.txt', '/home/pedro/winter2020/20PY/dumpFiles/1926070127capital_letters.txt', '/home/pedro/winter2020/20PY/dumpFiles/PEOPLE.txt', '/home/pedro/winter2020/20PY/dumpFiles/123.txt', '/home/pedro/winter2020/20PY/dumpFiles/information.txt', '/home/pedro/winter2020/20PY/dumpFiles/info1.txt', '/home/pedro/winter2020/20PY/dumpFiles/1923010312capital_letters.txt', '/home/pedro/winter2020/20PY/dumpFiles/调查结果.txt', '/home/pedro/winter2020/20PY/dumpFiles/info1_1.txt', '/home/pedro/winter2020/20PY/dumpFiles/1929050140capital_letters.txt', '/home/pedro/winter2020/20PY/dumpFiles/chocolate_snow_in_Switzerland.txt', '/home/pedro/winter2020/20PY/dumpFiles/infol.txt']
Reply
#4
The * in the call means that it is expanding the list into the individual elements. You can do the same for your list.

results = wc(*pathtoFiles)
wc is expecting something that looks like a filename (either a string or a Path object). But pathtoFiles is a list, so we have to open it up and just hand it the parts of the list, not the list itself.
Pedroski55 likes this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Could you explain each part of the code? Tsushida 2 1,466 Mar-20-2022, 08:19 AM
Last Post: Larz60+
  to import just part of a modulr Skaperen 11 4,665 Oct-15-2018, 02:16 AM
Last Post: Skaperen
  Import error when trying to import DDE (part of PyWin32) fbicalho 0 3,779 Apr-21-2018, 07:26 PM
Last Post: fbicalho

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020