codehaus


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Local access to a file, How To ?


On 29/07/2020 08:56, Steve wrote:
> I have a python program that reads and writes to files that are all within
> the folder that contains the python program.  There is now a second python
> program that is to be run to compile information in said files.
> 
> I am having difficulty trying to call the local supporting program from
> within the main program.  I would it would found easily because everything
> is all in the same folder.  Apparently, I have to decipher paths to do the
> task.
> 
> One problem is that the project is portable on a thumb drive and the path
> can change based on the computer on which the program is executed.  I look
> up use of path and I seem to get all absolute path instruction and not
> relative.
> 
> Where an I steering wrongly here.


Questions:
- is the second program kept in the same directory as the first, or 
somewhere else?
- by "call" do you mean "import", or...


If the program and the data files are in the same directory, then you 
will have noticed that there is no need for absolute addressing, ie

     open( "file.in" )

not

     open( "/home/me/Projets/this/file.in" )

The same applies to import-s. From program_A we can

     import program_B

in the same directory. No need for absolute paths, anywhere!

That is to say, Python works happily with the concept of the "current 
working directory", and when no absolute-path is provided assumes 'this 
directory' - or if a non-absolute-path is provided (doesn't commence 
with "/", in the case of Posix) prefixes the path from 'this directory'. 
Yes, there are many caveats beyond such a simple explanation, but that 
should be enough to get-going. Keeping 'everything' in the single 
directory, and always executing from there, should enable migration.
-- 
Regards =dn