4
''' Data class'''

import os.path
from random import Random

class TestData(object, Random):

    def FetchDataFromFile(self, filename):
        """ Open the file as read only """
        myfile = open(os.path.join(os.getcwd(),filename), 'r')
        """ read the information in the file """
        lines = myfile.read()
        ''' Remove the header as this will not be used '''
        header = lines[0] 
        lines.remove(header)
        return lines

私は得ています:

ImportError: パスという名前のモジュールがありません

ファイル「pyclasspath /Lib/Testdata.py」の 2 行目

os.path は、プロジェクトの他のすべてのクラスで機能しています。誰かが私がやっている間違いを指摘できますか?

このファイルをあるディレクトリから別のディレクトリに移動しました。それ以外は、このクラスと他のクラスに違いはありません。

4

2 に答える 2

4

import os代わりに正常に動作するはずです

import os
from random import Random

class TestData(object, Random):

    def FetchDataFromFile(self, filename):
        """ Open the file as read only """
        myfile = open(os.path.join(os.getcwd(),filename), 'r')
        """ read the information in the file """
        lines = myfile.read()
        ''' Remove the header as this will not be used '''
        header = lines[0] 
        lines.remove(header)
        return lines

余談ですが、あなたの方法は

def FetchDataFromFile(self, filename):
        """ Open the file as read only """
        return list(open(os.path.join(os.getcwd(),filename), 'r'))[1:]
于 2013-05-13T05:30:05.020 に答える
2

プロジェクトのパッケージ名は「Lib」で、Testdata モジュールを Lib に移動しました。Pythonはパッケージ名が気に入らなかったと思います。ライブラリに名前を変更し、現在は機能しています。エラーは import ステートメントとは関係ありません。import os.path と from os import path の両方が正常に動作します。

于 2013-05-13T06:21:10.827 に答える