[英]basic trouble opening a file with python object oriented programming script
I'm new to OOP and am having trouble writing and executing a basic script that will open and read a file. 我是OOP的新手,无法编写和执行将打开和读取文件的基本脚本。
I'm getting an error IOError: [Errno 2] No such file or directory: '--profile-dir'
when I run this. 我收到错误
IOError: [Errno 2] No such file or directory: '--profile-dir'
当我运行此IOError: [Errno 2] No such file or directory: '--profile-dir'
。 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 in with open(filename, "r")
, use with open(self.filename, "r")
to use the actual attribute you have defined in the class: 在某处定义了
filename = '--profile-dir'
,该名称with open(filename, "r")
使用, with open(self.filename, "r")
一起使用以使用您定义的实际属性在课堂里:
filename = "foob"
class testing(object):
def __init__(self, filename):
self.filename = filename
self.words = self.file_to_text()
def file_to_text(self):
print(filename)
with open(filename, "r") as file_opened:
text = file_opened.read()
words = text.split()
return words
Output: 输出:
foob
IOError: [Errno 2] No such file or directory: 'foob'
Your code will work fine using sys.argv once you make the change: 进行更改后,您的代码将可以使用sys.argv正常工作:
import sys
class testing(object):
def __init__(self, filename):
self.filename = filename
self.words = self.file_to_text()
def file_to_text(self):
with open(self.filename, "r") as file_opened:
text = file_opened.read()
words = text.split()
return words
alice = testing(sys.argv[1]).file_to_text()
print alice
:~$ python main.py input.txt
['testing']
If you want to use ./
put #!/usr/bin/env python
at the top and chmod +x
to make it executable. 如果要使用
./
请将#!/usr/bin/env python
放在顶部,并使用chmod +x
使其可执行。
You can also avoid calling read and splitting using itertools.chain: 您还可以避免使用itertools.chain调用read和split:
from itertools import chain
class testing(object):
def __init__(self, filename):
self.filename = filename
self.words = self.file_to_text()
def file_to_text(self):
with open(self.filename, "r") as file_opened:
return list(chain.from_iterable(line.split() for line in file_opened))
with open(filename, "r") as file_opened:
This reads from a global variable named filename
, not the one that you set in your initializer. 这将从名为
filename
的全局变量读取,而不是您在初始化程序中设置的变量 。 Presumably, it has the value '--profile-dir'
, so it tries to open a file with that name and throws an error when it doesn't exist. 大概它的值为
'--profile-dir'
,因此它尝试打开具有该名称的文件,并且在不存在该文件时引发错误。 You want to replace filename
with self.filename
to get the field in the class instance. 您想用
self.filename
替换filename
以获取类实例中的字段。
--profile-dir
in the run configuration settings and remove it. --profile-dir
并将其删除。 To pass the arguments from the command line, append the code below to your script 要从命令行传递参数,请将下面的代码附加到脚本中
if __name__ == '__main__': if len(sys.argv) > 1: alice = testing(sys.argv[1]).file_to_text()
This code uses linecache and reads any line that you want, without really opening the file. 此代码使用linecache并读取所需的任何行,而无需真正打开文件。 It is fast!
快!
import linecache
class Read_Line(object):
def __init__(self, file_input, line):
self.file_input = file_input
self.line = line
def Line_to_Text(self):
Words = linecache.getline(self.file_input, self.line)
Words = Words.split()
return Words
Test = Read_Line("File.txt", 3)
print Test.Line_to_Text()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.