Hello, When I'm using the 'open' function how should I specifiy the directory ? I'm getting an IOError and bit of confused! I'm not sure if my script is wrong or I've problems with specifying the directory.

input = open('New.txt', 'r')
text = input.read()
wordlist = text.split()
wordfreq = [wordlist.count(p) for p in wordlist]
dictionary = dict(zip(wordlist,wordfreq))
aux = [(dictionary[key], key) for key in dictionary]
aux.sort()
aux.reverse()
for a in aux: print a

If someone has a very simple version for the above script, please let me know. I'm trying to write a procedure that accepts a file name from the user, builds a frequency listing of the characters contained in the file, and prints a sorted and formatted character frequency table to the screen. Looking forward to your advise. Thank you.

Recommended Answers

All 3 Replies

    import os.path

    directory = r"C:\Python27\mystuff"  # Windows
    filename = "New.txt"
    path = os.path.join(directory, filename)

    # test
    print(path)  # result --> C:\Python27\mystuff\New.txt

    # then ...
    with open(path, 'r') as fin:
        text = fin.read()

Note: Windows allows you to replace \ with / in the directory string

In your code do not use Python function names like input for variable names!

For word or character counting use Counter from the Python module collections ...

''' char_frequency_tuple3.py
create a list of (char, frequency) tuples
using he most_common() method of collections.Counter
'''

import collections

# sample text, could come from a file
text = """\
add two even numbers together and you get an even number
add two odd numbers together and you get an even number
the only way to get an odd number is to add an even number to an odd one
"""

# remove newline characters
text = text.replace('\n', '')

# this creates a list of (char, frequency) tuples
# by default the list is sorted by frequency
mylist = collections.Counter(text).most_common()

print("Sorted by frequency:")
for character, freq in mylist:
    print("%r  %d" % (character, freq))

''' result...
Sorted by frequency:
' '  37
'e'  23
'n'  19
'd'  14
'o'  14
't'  13
'a'  11
'r'  8
'u'  8
'b'  6
'm'  6
'g'  5
'v'  4
'y'  4
'h'  3
's'  3
'w'  3
'i'  1
'l'  1
'''
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.