使用python面向对象编程脚本打开文件时遇到的基本问题
我是OOP新手,在编写和执行将打开和读取文件的基本脚本时遇到困难 运行此操作时,我收到一个错误使用python面向对象编程脚本打开文件时遇到的基本问题,python,oop,sys,Python,Oop,Sys,我是OOP新手,在编写和执行将打开和读取文件的基本脚本时遇到困难 运行此操作时,我收到一个错误IOError:[Errno 2]没有这样的文件或目录:'--profile dir'。这有什么问题,我该如何解决 class testing(object): def __init__(self, filename): self.filename = filename self.words = self.file_to_text() def file
IOError:[Errno 2]没有这样的文件或目录:'--profile dir'
。这有什么问题,我该如何解决
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
另外,如果我想从命令行执行这个命令,这些调整应该可以让它工作,对吗
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
提前感谢各位。在某个地方定义了一个
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
输出:
foob
IOError: [Errno 2] No such file or directory: 'foob'
进行更改后,使用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']
如果你想使用/
放#/usr/bin/env python
位于顶部,并chmod+x
使其可执行
还可以使用itertools.chain避免调用读取和拆分:
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))
在某个地方,您定义了一个
filename='--profile dir'
,它在中使用open(filename,“r”)
,使用和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
输出:
foob
IOError: [Errno 2] No such file or directory: 'foob'
进行更改后,使用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']
如果你想使用/
放#/usr/bin/env python
位于顶部,并chmod+x
使其可执行
还可以使用itertools.chain避免调用读取和拆分:
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))
在某个地方,您定义了一个
filename='--profile dir'
,它在中使用open(filename,“r”)
,使用和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
输出:
foob
IOError: [Errno 2] No such file or directory: 'foob'
进行更改后,使用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']
如果你想使用/
放#/usr/bin/env python
位于顶部,并chmod+x
使其可执行
还可以使用itertools.chain避免调用读取和拆分:
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))
在某个地方,您定义了一个
filename='--profile dir'
,它在中使用open(filename,“r”)
,使用和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
输出:
foob
IOError: [Errno 2] No such file or directory: 'foob'
进行更改后,使用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']
如果你想使用/
放#/usr/bin/env python
位于顶部,并chmod+x
使其可执行
还可以使用itertools.chain避免调用读取和拆分:
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))
--profile dir
,然后将其删除如果uuuu name_uuuu=='\uuuuuuu main\uuuuuu':
如果len(sys.argv)>1:
alice=测试(sys.argv[1])。文件到文本()
--profile dir
,然后将其删除如果uuuu name_uuuu=='\uuuuuuu main\uuuuuu':
如果len(sys.argv)>1:
alice=测试(sys.argv[1])。文件到文本()
--profile dir
,然后将其删除如果uuuu name_uuuu=='\uuuuuuu main\uuuuuu':
如果len(sys.argv)>1:
alice=测试(sys.argv[1])。文件到文本()
--profile dir
,然后将其删除如果uuuu name_uuuu=='\uuuuuuu main\uuuuuu':
如果len(sys.argv)>1:
alice=测试(sys.argv[1])。文件到文本()
filename
的全局变量读取,而不是在初始值设定项中设置的变量。据推测,它的值为--profile dir,因此它尝试打开具有该名称的文件,并在该文件不存在时抛出错误。您希望将filename
替换为self.filename
,以获取类实例中的字段
它从名为filename
的全局变量读取,而不是在初始值设定项中设置的变量。据推测,它的值为--profile dir,因此它尝试打开具有该名称的文件,并在该文件不存在时抛出错误。您希望将filename
替换为self.filename
,以获取类实例中的字段
它从名为filename
的全局变量读取,而不是在初始值设定项中设置的变量。据推测,它的值为--profile dir,因此它尝试打开具有该名称的文件,并在该文件不存在时抛出错误。您希望将filename
替换为self.filename
,以获取类实例中的字段
它从名为
filename
的全局变量读取,而不是在初始值设定项中设置的变量。据推测,它的值为--profile dir,因此它尝试打开具有该名称的文件,并在该文件不存在时抛出错误。您想将filename
替换为self.filename
,以获取类实例中的字段。此代码使用linecache并读取所需的任何行,而不真正打开文件。它很快
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()
这段代码使用linecache并读取您想要的任何行,而不需要真正的openi