Tuesday, 15 July 2014

Python 3.4 - Opening a file in a module with a different directory -


I have a package that looks like the following

  package / __init__.py Module     in the module   

  def function (file_name): open (file_name) As with F: # do stuff  

Somewhere else in a arbitrary directory I have a python file that looks like something

  package package import Please. Function ("some_file .txt")  

But upon turning it on, it is giving me the FileNotFoundError: [Errno 2] There is no such file or directory: "some_file. Txt ".

The problem is that the complete path of some_file.txt may look like c: \ user \ user \ document \ some_file.txt , but < Code> package.function Path complete path is something like c: \ user \ user \ document \ package \ some_file.txt . Is there a way I can create this from which to open the file by calling package.function from some file outside the package directory? Can I join?

Sorry if my terminology is really ambiguous, then I am actually unfamiliar with the os stuff.

Edit: The exact file setup looks like this:

  directory / foo.py package / __init__.py module.py a Other directories / bar.txt  

and foo

< P>

Class = "post-text" itemprop = "text">

I think you are missing points.

It is not important that where the source code is, the relative path (and the pure file name is relative path) are always interpreted relative to that directory in the process of program Walks (ie directory, when you type dragon c: \ path \ to \ my \ python \ code \ code.py )


No comments:

Post a Comment