0

ファイルを開くのに問題があります.python(2.7)は、そのようなファイルはないと言っていますが、あります。

        csvData = None
        csvHeader = None
        os.chdir("../result/files/")
        for fileName in os.listdir("."):
            if fileName.endswith(".csv"):
                print fileName
                with open("../result/files/"+fileName, 'rb') as csvFile:
                    readerCsv = reader(csvFile, delimiter = ';')
                    csvHeader = readerCsv.next()
                    _unused = readerCsv.next()
                    self.data = list.append(list(readerCsv))

このコードは、5 つの csv ファイルを読み取り、その内容をデータに保存することになっています (最初の行を除く)

最初に、chdir は Stats20120903.csv を見つけたことを教えてくれました。次に、プログラムにそのファイルを開くように依頼しましたが、エラーが発生しました。

IOError: [Errno 2] No such file or directory: '../files/Stats20120903.csv'

なぜ?

ありがとう

4

3 に答える 3

1

os.chdir does not tell you what is in the directory, it changes the current working path. All relative file path you give are relative to the working dir.

You should either

  • remove the os.chdir("../result/files/") and call os.listdir("../result/files/") or
  • not add "../result/files/" to the filename you want to open.
于 2013-03-04T12:09:05.320 に答える
1

行う

with open(fileName, 'rb') as csvFile:

変更先のディレクトリを基準にして探すのではなく。

于 2013-03-04T12:06:55.513 に答える
0

os.path.abspath()を使用して、Python が使用しようとしている実際のパスを調べることができます。

于 2013-03-04T12:07:59.673 に答える