简体   繁体   English

CSV模块读入文件,位于其他文件夹(例如,我的下载文件夹)中

[英]CSV Module Reading In Files, which are in some other folder (e.g. my Downloads folder)

so I want to use the csv module in order to read in some csv.files but I get the following errors with the given code: 所以我想使用csv模块以读取一些csv.files,但是在给定的代码中出现以下错误:

########################### IMPORT MODULES ############################

import sys, time, glob, os, csv, student

######################### DEFINE PARAMETERS ###########################

timings_csv_file      = './timings.csv'
inputs_csv_file       = './inputs.csv'
testing_pair_csv_file = './testing_pair.csv'

############## LOAD TIMINGS INFORMATION AND TEST PAIR #################

csv_reader = csv.reader(open(timings_csv_file, 'rb'), delimiter=',')
timings = [int(element) for element in csv_reader.next()]

csv_reader = csv.reader(open(testing_pair_csv_file, 'rb'), delimiter=',')
testing_pair = [long(element) for element in csv_reader.next()]

csv_reader = csv.reader(open(inputs_csv_file, 'rb'), delimiter=',')
inputs = [long(element) for element in csv_reader.next()]

##################### PERFORM TIMING ATTACK ###########################

key = student.perform_timing_attack(inputs, timings, testing_pair)

######################## OUTPUT RESULTS ###############################

keyhex = ",".join(["%02X" % (key >> 64-(8*(i+1)) & 0x00000000000000FF ) for i in range(64/8) ])
print keyhex

################### WRITE RESULTS TO A FILE ###########################

keyF = open( "./key.txt", "w" )
keyF.write(keyhex)
keyF.close()

I get the upcoming Error Message: 我收到即将出现的错误消息:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/Amine/anaconda/lib/python2.7/site-packages/spyder/utils/site/sitecustomize.py", line 866, in runfile
    execfile(filename, namespace)
  File "/Users/Amine/anaconda/lib/python2.7/site-packages/spyder/utils/site/sitecustomize.py", line 94, in execfile
    builtins.execfile(filename, *where)
  File "/Users/Amine/Downloads/SIKA_Aufgabe_2/ga24feb_timings/framework/main.py", line 13, in <module>
    csv_reader = csv.reader(open(timings_csv_file, 'rb'), delimiter=',')
IOError: [Errno 2] No such file or directory: './timings.csv'

I think Python can not find the files in the current directory where it is actually looking for or running since I said: "./file.csv". 我认为Python自从我说过:“ ./ file.csv”后,就无法在当前正在查找或运行的当前目录中找到文件。 As far as I know it is looking in its current directory. 据我所知,它正在其当前目录中查找。 I know, that if I enter the whole path of the csv-files, it will work, but I want to make it independent of the file-path, thus the code has to be able to work always with the data in its own path. 我知道,如果我输入csv文件的整个路径,它将起作用,但是我想使其独立于文件路径,因此代码必须能够始终使用其自身路径中的数据进行工作。

So how do I have to change the path of Python in order to leave the csv-files where they are and still be able to use "./file.csv". 因此,我必须如何更改Python的路径才能将csv文件保留在原位置,并且仍然可以使用“ ./file.csv”。 Eg when I check what path Python is actually looking for by using "sys.path[0]" I get the output " '' " which I do not really understand because what the heck is '' for a path? 例如,当我使用“ sys.path [0]”检查Python实际在寻找什么路径时,得到的输出“”并不真正理解,因为路径的含义是什么? Moreover, I tried to set my PYTHONPATH but I think the PYTHONPATH only involves data import for modules (is this right?). 此外,我试图设置我的PYTHONPATH,但我认为PYTHONPATH仅涉及模块的数据导入(对吗?)。

So what should I do to set the path Python is looking for such that "./file.csv" works for me. 所以我应该怎么设置Python正在寻找的路径,以使“ ./file.csv”对我有用。

Thanks! 谢谢!

If you know that the files you are processing are always in the same path as your script as indicated by your statement "thus the code has to be able to work always with the data in its own path.", you can just call the file with its name (without the slash): 如果您知道正在处理的文件始终与脚本的路径相同,如语句“因此代码必须能够始终使用其自身路径中的数据工作”所示,则只需调用该文件即可。其名称(不带斜杠):

timings_csv_file      = 'timings.csv'
inputs_csv_file       = 'inputs.csv'
testing_pair_csv_file = 'testing_pair.csv'

this will work when you directory looks like this: 当您的目录如下所示时,它将起作用:

dir/
....script.py
....timings.csv
....inputs.csv
....testing_pair.csv

On the other hand if you want to go up one hierarchy level (i'm not quite sure if that was what you were going for?) you could do this: 另一方面,如果您想上一个层次结构级别(我不太确定这是否是您想要的?),可以这样做:

timings_csv_file      = '../timings.csv'
inputs_csv_file       = '../inputs.csv'
testing_pair_csv_file = '../testing_pair.csv'

this will work when your folder structure looks like this: 当您的文件夹结构如下所示时,它将起作用:

dir/
....timings.csv
....inputs.csv
....testing_pair.csv
....python/
    ....script.py

Instead of, 代替,

timings_csv_file      = './timings.csv'
inputs_csv_file       = './inputs.csv'
testing_pair_csv_file = './testing_pair.csv'

use 采用

import os
current_path = os.getcwd() 
timings_csv_file      = current_path + '\\timings.csv'
inputs_csv_file       = current_path + '\\inputs.csv'
testing_pair_csv_file = current_path + '\\testing_pair.csv'

This, gets the current working directory of your script and passes the complete path to library. 这将获取脚本的当前工作目录,并将完整路径传递到库。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM