Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/341.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用python面向对象编程脚本打开文件时遇到的基本问题_Python_Oop_Sys - Fatal编程技术网

使用python面向对象编程脚本打开文件时遇到的基本问题

使用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

我是OOP新手,在编写和执行将打开和读取文件的基本脚本时遇到困难

运行此操作时,我收到一个错误
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