I'm new to OOP and am having trouble writing and executing a basic script that will open and read a file.
I'm getting an error IOError: [Errno 2] No such file or directory: '--profile-dir'
when I run this. What's wrong with this and how should I fix it?
class testing(object):
def __init__(self, filename):
self.filename = filename
self.words = self.file_to_text()
def file_to_text(self):
with open(filename, "r") as file_opened:
text = file_opened.read()
words = text.split()
return words
alice = testing("alice.txt").file_to_text()
print alice
Also, if I'd like to be able to make this executable from the command line, these tweaks should make it work, right?
import sys
...
alice = testing(sys.argv[1]).file_to_text()
print alice
line to actually input in command line to run it-----> ./testing.py alice.txt
thanks in advance guys.
Somewhere you have a
filename = '--profile-dir'
defined, that is being used inwith open(filename, "r")
, usewith open(self.filename, "r")
to use the actual attribute you have defined in the class:Output:
Your code will work fine using sys.argv once you make the change:
If you want to use
./
put#!/usr/bin/env python
at the top andchmod +x
to make it executable.You can also avoid calling read and splitting using itertools.chain: