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 ?
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
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)
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
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.
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")
Related
I want to read a file using Chaquopy. After some errors like "PermissionError" I wrote the same code in Python Idle and Chaquopy:
This code in Idle writes 'True' in output.
import xlrd, os
filename = 'F:/q1.xlsx'
print(os.path.isfile(filename))
And this one in Chaquopy outputs 'False':
import xlrd, os
filename = 'F:/q1.xlsx'
self.findViewById(R.id.label).setText(str(os.path.isfile(filename)))
I tried all the described combinations of '\' and '/'. It also cannot open a file in the same folder with Python activity if I write only its relative path. How to make Chaquopy work with files correctly?
You can access files either using pkgutil or by using the extractPackages setting. See the documentation for details.
I am trying to read from a .txt file located within the project structure. After the app has been compiled to the device (tested on both Android and iOS), I begin by checking if the file exists. It does not seem to.
fileAccess.ts:
import fs = require("file-system");
export class FileAccess
{
public data(filePath: string)
{
let exists = fs.File.exists(filePath);
console.log(exists);
}
}
test.txt (located in same directory as fileAccess.ts):
1;DAC
Calling data("./test.txt"); on an instance of FileAccess, the console prints false.
I assume that either I am referencing the file wrong, or the file is not being copied to the device. But which is it, and how do I fix it?
You could use knownFolders for that. Assuming test.txt is in the root of the app folder:
let appPath = fs.knownFolders.currentApp().path;
let myTextFile = appPath + "/test.txt"
I'm trying to create a partition called "subdata" under "/data". But it's failing.
The steps I tried and the failure results are mentioned below.
File dir =new File(/data/subdata/");
boolean success = dir.mkdir();
Here, the "success" value is found "false".
File dir= context.getDir("/data/subdata",Context.MODE_WORLD_READABLE);
Here, I get "java.lang.IllegalArgumentException: File app_/data/subdata/ contains a path separator"
Please, help me in creating this subfolder under /data/ partition.
I solved my need in the below manner.
As I mentioned in the question that, if I create the sub folder manually, I'm able to read and write to that subfolder in the /data.
So, I created this subfolder through init.rc (as I mentioned, I have the build code also). As I'm more fluent in Linux than in Android, I fixed it through init.rc. Now I'm able to read/write to that folder through my android code.
I have installed QPython in my Android mobile.
I written a statement in the QEdit to read a text file from the below path
/storage/emulated/0/com.hipipal.qpyplus/script3/File1.txt
I used the below statement
fob=open('/storage/emulated/0/com.hipipal.qpyplus/script3/File1.txt','r')
fob.read()
If I run the statement, it is throwing error as:
IOError:[Errno 2] No such file or directory: '/storage/emulated/0/com.hipipal.qpyplus/script3/File1.txt'
1|uo_a116#cancro:/ $
Is the above statement correct?
fob=open('File1.txt','r')
Is not working in version 1.0.4.
fout=open('File2.txt','w')
Was working on version 0.9.6, but is not working in 1.0.4.
The "error" is Read only file system.
It looks like restrictions in the (new 1.0.4) file system library. I post a mail to the editor, but no answer at this time.
For testing, try to write absolute path to your files pointing, for example, to sdcard (/sdcard/out.txt).
I had problems on this versions (>=1.0.4) because launch process of script changes and execution directory is not the same as script directory.
I had to change my scripts to point to absolute paths.
It was tested with qpython developer.
Check this link:
https://github.com/qpython-android/qpython.org/issues/48
You can also try as simple as:
fob=open('File1.txt','r')
fob.read()
Just if the script is in the same folder of the file.
You can change the current working directory to path with script before read file:
import os
os.chdir(os.path.dirname(os.path.abspath(__file__)))
I am trying to write a file in the internal storage directory of my application with the following step :
1) Initializing my "jni library" in Acivity Class :
MyLib mylib = new MyLib();
2) Give the internal storage path by calling getFilesDir in my Activity Class:
mylib.setSavePath(getFilesDir());
3) Call a method mylib.save() from my library which is doing the following in c++:
Open the file which i want to write with :
fp = fopen(pathtotheinternalstorage+filename,"w");
if (!fp) {
SetError(XML_ERROR_FILE_NOT_FOUND, filename, 0);
return _errorID;
}
The file path is correct : /data/data/com.myapp/files/myfile.xml
But fopen fails, i dont know what i am doing wrong.
If i write with some java code (openFileOutput), it is working well.
Thanks for your help.
More information would be helpful. Based on the path, I'm assuming you're on a Linux box. I would first check the permissions of the path. A few suggestions below
1) Try opening a file in your home directory and see if you have the same issue.
2) Try setting the file world writable using chmod 777 and see if that fixes the problem. Remember setting world writable is really INSECURE and you should change it to something more restrictive once you've found the problem.
I found the answer, i was using a library that was redefinig fopen to read inside asset folder.
#define fopen(name, mode) android_fopen(name, mode)
now it s working