0

What is the proper way to input file path name in the terminal? Should you type in the full path or something else? I wrote code that's supposed to check some sort of cardinality in files. But no matter how I input the path name it always spits out the same result (0:0). The code should be sound, but I'm thinking it's the path name I'm typing in wrong.

import os

wide_count = 0
long_count = 0


def card(file_name):
    number_of_lines = 0
    longest_line = 0
    f = open(file_name, "r")
    for line in f:
        number_of_lines+= 1
        if len(line) > longest_line:
           longest_line = len(line)
    f.close()
    if longest_line > number_of_lines:
       return 1
    else:
       return 0          

directory = raw_input("Input file pathname: ")

for(current, sub, files) in os.walk(directory):
    for f in files:
        if card(os.path.join(current, files)) == 1:
           wide_count += 1
        else:
           long_count += 1     

print "{0:d} : {1:d}".format(wide_count, long_count)           
monolith937
  • 429
  • 1
  • 4
  • 13
  • Can you edit-in your input as well? That way we can check if it's the problem caused by your input method or something else. – Lafexlos Nov 14 '16 at 12:06
  • Also, are you sure it is `os.path.join(current, files)` instead of `os.path.join(current, f)`? – Lafexlos Nov 14 '16 at 12:08
  • Yeah, you were right about `f` instead of `files`. But the problem still persists. This is how i input the path name: `Input file pathname: "/home/dule/Desktop/python"` `0 : 0` – monolith937 Nov 14 '16 at 12:12
  • You should check [this one](http://stackoverflow.com/questions/1945920/why-doesnt-os-path-join-work-in-this-case). tl;dr: don't use forward slash at beginning of your string. – Lafexlos Nov 14 '16 at 12:13
  • Well, it passed without errors, but it still keeps 0:0 as the result, this can't be right. I'm almost definitely sure it's not the code. – monolith937 Nov 14 '16 at 12:21
  • Then you should add some prints here and there to see if you get expected values. Like `print file_name` inside of `card` method and `print line` right under `line in f:` etc. – Lafexlos Nov 14 '16 at 12:24
  • Surprisingly, it prints out absolutely nothing. The output is still the same for some reason – monolith937 Nov 14 '16 at 12:28
  • so.. `print os.walk(directory)` or right inside of that line, `print files` etc. Since you are getting 0:0, your code doesn't get inside of `for f in files:` which means `files` is empty so most likely your input is flawed. – Lafexlos Nov 14 '16 at 12:29
  • `` Is this expected? – monolith937 Nov 14 '16 at 12:31

1 Answers1

0

I figured out the problem: when prompted for the input, you should actually put the absoulte pathname starting with a slash:

Instead of

home/_acc/Desktop/my_folder

it should be

/home/my_acc/Desktop/my_folder

monolith937
  • 429
  • 1
  • 4
  • 13