0

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:

########################### 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". 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.

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". E.g. 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? Moreover, I tried to set my PYTHONPATH but I think the PYTHONPATH only involves data import for modules (is this right?).

So what should I do to set the path Python is looking for such that "./file.csv" works for me.

Thanks!

SolingerMUC
  • 33
  • 1
  • 8
  • Are you trying to step up one directory from where the script is running? In which case you could `import os`, then `directory = os.path.dirname(__file__)` then `file_to_read = os.path.join(directory, '../timings.csv')`. I'm not sure about a more general solution that's independent of the script's location. – roganjosh Dec 01 '16 at 11:24
  • it is searching in `/Users/Amine/anaconda/lib/python2.7/site-packages/spyder/utils/site/` and that's why you are getting that error. – Prajwal Dec 01 '16 at 11:31
  • Yes, how do you know that. I changed my current working directory of python with the following " os.chdir('/Users/Amine/Downloads/SIKA_Aufgabe_2/ga24feb_timings')" with which I was able to step up one directory. But I still get the error message. – SolingerMUC Dec 01 '16 at 11:33
  • @Prajwal: so How can I change it to search **/Users/Amine/Downloads/SIKA_Aufgabe_2/ga24feb_timings** instead? – SolingerMUC Dec 01 '16 at 11:35
  • when there's relative path, python starts from location of current executing script. I found a very useful post here. Check this out. http://stackoverflow.com/questions/1270951/python-how-to-refer-to-relative-paths-of-resources-when-working-with-code-repo – Prajwal Dec 01 '16 at 11:40

2 Answers2

0

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
Christian W.
  • 2,532
  • 1
  • 19
  • 31
  • Hey Christian! First of all thanks for the solutions! It is true, that the csv. files are one hierarchy level above my current working directory (cwd). And if I use your method with `timings_csv_file = '../timings.csv' inputs_csv_file = '../inputs.csv' testing_pair_csv_file = '../testing_pair.csv'`it works like a charm :). But what is then the using case of `./file.csv` if I can use `../file.csv` in the case that my files are one directory level above and `file.csv`if they are in the current working directory. Already helped me a lot! Thank you – SolingerMUC Dec 01 '16 at 11:43
0

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.

Prajwal
  • 3,930
  • 5
  • 24
  • 50
  • Hey Prajwal! Thank you, this worked fine. But the thing is, I am not allowed to change the code I quoted above. Which is somehow the **main.py** file. Can you explain me the difference between using something like: `timings_csv_file = './timings.csv'` compared to `timings_csv_file = 'timings.csv'` so basically without the dot slash ? I always thought, that when typing `./files.type` I would be saying to look in the current directory it is in ? – SolingerMUC Dec 01 '16 at 12:05
  • @ Prajwal: Yes using only `timings.csv` works the same as with `./` in front of it. So I guess it has no effect? What do you say? – SolingerMUC Dec 01 '16 at 16:07
  • That's what I was thinking. You need to specify absolute path. Else keep the files in `frameworks`. – Prajwal Dec 01 '16 at 16:11
  • Thanks a lot! Meaning I have to have data and code in the same directory, which is actually a bad habit, but since I am not allowed to change it, I'll have to leave it like that. – SolingerMUC Dec 01 '16 at 16:27
  • Yes. And `./` and `just_name` shouldn't be any different 'cause Python resolves both to same path. I won't explain it here, it's too big for a comment. – Prajwal Dec 01 '16 at 17:13