2

I can't run my python program on my android device using qpython3.

The first step in he program is to create a text file to save data to it. But I get an I/O error (file system read only)

This is the function used to create / or be sure that the file exists easily.

def filecreate(file):       # creates the text file
    f = open(file, 'a')
    print('file created successfully\n')
    print()
    f.close()

How to overcome this problem in android ?

Bhugy
  • 711
  • 2
  • 8
  • 23
salah othman
  • 23
  • 1
  • 7

5 Answers5

1

QPython by default runs your programs from the root directory which is not writable by non-root users. If you check the ftp utility (under About menu) you will find the name of the directory that is writable by QPython. On my HTC it is:

/storage/emulated/0/com.hipipal.qpyplus

So you need to do something along the lines of:

RootPath='/storage/emulated/0/com.hipipal.qpyplus'
...
import os
f = open(file, os.path.join(RootPath,'a'))
...

Example of a similar problem with QPython and SQLite3

Community
  • 1
  • 1
CyberFonic
  • 3,957
  • 1
  • 21
  • 21
1

Just create the file with QPython before use in your script. You can create an txt file with QPython. In My case I create the file whith name "pontoText.txt" Inside folder "MyPythonScripts" (Im create that folder to put my python files) so the file is in the same folder of the script and are created in QPython Now an example code im make ton test:

import time
import os
a = input("1 to save date on file or 2  too see records on file. ")
folder= '/storage/emulated/0/MyPythonScripts'
if(a == "1"):
    pontoTxt=open(os.path.join(folder,'pontoText.txt'),'w')
    pontoTxt.write(time.strftime("%I %M %p on %A, %B %e, %Y"))
if(a == "2"):
    pontoTxt=open(os.path.join(folder,'pontoText.txt'),'r')
    exibe = pontoTxt.read()
    print(exibe)
0

Your app may not have required permission to manage that file. Check your manifest, specify Android version and file path. Check documentation: http://developer.android.com/training/basics/data-storage/files.html

Konstantin Berkov
  • 1,193
  • 3
  • 14
  • 27
0

You have to actually find the file path, as it doesn't infer the directory that the file is in as the destination directory, unlike in Python on the PC.

0

Use what they're changed. The file you have is inside /accounts/mastercard root and the file you're inside is /accounts/main.py you only need to use is this /mastercard/xxxxx like my code

from django.shortcuts import render
from django.http import HttpResponse
from . import models
# Create your views here.
def index(request):
    return render(request,"mastercard/Templates/mastercard/Base.html")
def about(request):
    return render(request,"mastercard/Templates/mastercard/index.html")
tripleee
  • 175,061
  • 34
  • 275
  • 318