为什么我无法在android上使用python创建文件
问题描述:
我无法使用qpython3在我的android设备上运行我的python程序。为什么我无法在android上使用python创建文件
他程序的第一步是创建一个文本文件来保存数据。但我得到一个I/O错误(文件系统只读)
这是用来创建/或确保该文件很容易存在的函数。
def filecreate(file): # creates the text file
f = open(file, 'a')
print('file created successfully\n')
print()
f.close()
如何在android中克服这个问题?
答
您的应用程序可能没有必要的权限来管理该文件。检查清单,指定Android版本和文件路径。检查文档:http://developer.android.com/training/basics/data-storage/files.html
答
您必须实际找到文件路径,因为它不会推断文件作为目标目录所在的目录,这与PC上的Python不同。
答
默认情况下,QPython从根目录运行你的程序,这是非root用户不可写入的。如果您检查ftp实用程序(在关于菜单下),您将找到可由QPython写入的目录的名称。在我的HTC是:
/storage/emulated/0/com.hipipal.qpyplus
所以,你需要做的线沿线的东西:
RootPath='/storage/emulated/0/com.hipipal.qpyplus'
...
import os
f = open(file, os.path.join(RootPath,'a'))
...