+ 1

How can I access files (r or w) from a script using qpython on android?

The simple "put it in the same folder" method and then something like "with open (file) as... " doesn't work, somehow the file can't be found... Also going with the whole path hasn't worked so far.

26th May 2018, 9:26 AM
HonFu
HonFu - avatar
8 Réponses
+ 2
I'm wondering if anyone will ever look at this old question of mine again, but: I have found a line of code on stack overflow that actually solves the path issue in QPython, so for all it's worth, I want to just share it: import os os.chdir(os.path.dirname( os.path.abspath(__file__))) After that line, you can read and write files without naming the full path, just as you'd expect to.
14th Mar 2019, 12:05 PM
HonFu
HonFu - avatar
+ 6
Ok I got it. when I run the script it is being run in the directory /. to solve the problem do this: import os path = '/storage/emulated/0/qpython/scripts3/' # you can change this to your own path os.chdir(path) # then try to read the file with open('test.py') as f: print(f.read()) It works for me.
26th May 2018, 10:17 AM
Ulisses Cruz
Ulisses Cruz - avatar
+ 1
would you like to show us your code?
26th May 2018, 9:36 AM
Ulisses Cruz
Ulisses Cruz - avatar
+ 1
Basically it's just this: with open('text.py') as d: text=d.read() And the same textbook method for writing. It works just fine on windows and python 3.6, but qpython somehow can't do this... It can't finde the file, although it should work if I have the script and the 'text.py' file both in the scripts3 folder, right?
26th May 2018, 9:44 AM
HonFu
HonFu - avatar
+ 1
What are you doing first reading or writing?
26th May 2018, 9:53 AM
Ulisses Cruz
Ulisses Cruz - avatar
+ 1
First I read, then do a simple change (like adding something to the string) then saving (adding "w") using the same pattern. It is just a test run to learn how to access files, because I want two scripts to run on android, that already work on windows pc. I find this a bit confusing. Okay, not everything in qpython is working yet, I get that; but what I'm trying to do is REALLY basic!
26th May 2018, 9:59 AM
HonFu
HonFu - avatar
+ 1
I created two py-files on my pc, one that writes and reads, one that is written in and read from, having them in the same folder. So after I checked it worked, I put the same two files into 'scripts3', ran it and - it didn't work.
26th May 2018, 10:16 AM
HonFu
HonFu - avatar
+ 1
Ha - thank you, you solved my problem!
26th May 2018, 11:39 AM
HonFu
HonFu - avatar