Frage

On my macbook air running OSX Mavericks (I'm almost certain this wasn't happening the other day on a PC running Windows 7 running virtually identical code) the following code gives me the following error.

import numpy as np

massFile='Users/BigD/Dropbox/PhD/PPMS/DATA/DB/HeatCap/HeatCapMass.txt'
print massFile

sampleInfo=np.genfromtxt(fname=massFile,skip_header=2,usecols=(2,3,4),dtype=float)

massfile is printed out as expected as 'Users/BigD/Dropbox/PhD/PPMS/DATA/DB/HeatCap/HeatCapMass.txt'

but I get the error

Traceback (most recent call last):
  File "test.py", line 7, in <module>
    sampleInfo=np.genfromtxt(fname=massFile,skip_header=2,usecols=(2,3,4),dtype=float)
  File "//anaconda/lib/python2.7/site-packages/numpy/lib/npyio.py", line 1317, in genfromtxt
    fhd = iter(np.lib._datasource.open(fname, 'rbU'))
  File "//anaconda/lib/python2.7/site-packages/numpy/lib/_datasource.py", line 145, in open
    return ds.open(path, mode)
  File "//anaconda/lib/python2.7/site-packages/numpy/lib/_datasource.py", line 477, in open
    return _file_openers[ext](found, mode=mode)
IOError: [Errno 2] No such file or directory: '/Users/BigD/Dropbox/PhD/PPMS/Users/BigD/Dropbox/PhD/PPMS/DATA/DB/HeatCap/HeatCapMass.txt'

it appears to be trying to be using half of the path and then adding the full path file to the end of it.

Does anyone know why this is happening or can suggest a work around?

War es hilfreich?

Lösung

The path you're supplying in massFile is relative to the directory you're executing the script in.

To see where you are, just type pwd in your shell. In your case, it will return /Users/BigD/Dropbox/PhD/PPMS/. So this value is silently prepended to your path:

massFile='/Users/BigD/Dropbox/PhD/PPMS/Users/BigD/Dropbox/PhD/PPMS/DATA/DB/HeatCap/HeatCapMass.txt'

This is also the value you're seing in your traceback.

There are two ways to fix this:

To mark a path to be absolute just prefix the path with a /:

massFile='/Users/BigD/Dropbox/PhD/PPMS/DATA/DB/HeatCap/HeatCapMass.txt'

or to keep it relative you have to remove the unneeded bits:

massFile='DATA/DB/HeatCap/HeatCapMass.txt'

I would suggest picking the latter, that way you can move the project around without breaking all your paths.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top