0

I have a PyDev project in eclipse consisting of a "src" directory and an "rsc" directory.

I would like to read/write files in the "rsc" dir. If i give for example the following command in .py file in the "src" dir:

numpy.savetxt("rsc/test.txt", temp, fmt='%3.15f', delimiter=' ')

I get an error saying "No such file: rsc/test.txt", (Giving the absolute path (i-e "home/.../test.txt") works.)

This works for java projects. How can I do this for python projects? Is this problem specific to eclipse?

To clarify, my dir structure is as follows: proj_dir -> src -> file.py, proj_dir -> rsc -> test.txt I am running a file in src e-g "file.py"

slmnkhokhar
  • 87
  • 1
  • 7
  • What does your directory structure look like? This has absolutely nothing to do with PyDev vs. JDT, and everything to do with what Python considers the [current working directory](http://en.wikipedia.org/wiki/Working_directory) vs. what Java does (and thus how it evaluates relative paths). – aruisdante Jul 25 '14 at 01:03
  • my dir structure is as follows: proj_dir -> src -> file.py, proj_dir -> rsc -> test.txt I am running a file in src e-g "file.py" – slmnkhokhar Jul 25 '14 at 17:35
  • You should edit that information into your question – aruisdante Jul 25 '14 at 18:01
  • possible duplicate of [Python - how to refer to relative paths of resources when working with code repository](http://stackoverflow.com/questions/1270951/python-how-to-refer-to-relative-paths-of-resources-when-working-with-code-repo) – aruisdante Jul 25 '14 at 18:01

1 Answers1

1

instead of using :

numpy.savetxt("rsc/test.txt", temp, fmt='%3.15f', delimiter=' ')

you can use :

import os, inspect
this_file = os.path.abspath(inspect.getfile(inspect.currentframe()))

project_dir = os.path.dirname(os.path.dirname(this_file))
numpy.savetext(os.path.join(project_dir,"rsc/test.txt"), temp, fmt='%3.15f', delimiter=' ')

This will always work if your src and rsc directories share the same parent.

Tony Suffolk 66
  • 9,358
  • 3
  • 30
  • 33