【问题标题】:Why can't I create a file on android using python为什么我不能使用 python 在 android 上创建文件
【发布时间】:2016-05-13 03:20:19
【问题描述】:

我无法使用 qpython3 在我的 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 python-3.x qpython3


    【解决方案1】:

    QPython 默认从非 root 用户不可写的根目录运行你的程序。如果您检查 ftp 实用程序(在 About 菜单下),您将找到 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'))
    ...
    

    Example of a similar problem with QPython and SQLite3

    【讨论】:

      【解决方案2】:

      在您的脚本中使用之前,只需使用 QPython 创建文件。 您可以使用 QPython 创建一个 txt 文件。 在我的情况下,我创建了名为“pontoText.txt”的文件 内部文件夹“MyPythonScripts”(我创建该文件夹来放置我的 python 文件) 所以该文件位于脚本的同一文件夹中,并在 QPython 中创建 现在是我做吨测试的示例代码:

      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)
      

      【讨论】:

        【解决方案3】:

        您的应用可能没有管理该文件所需的权限。检查您的清单,指定 Android 版本和文件路径。查看文档:http://developer.android.com/training/basics/data-storage/files.html

        【讨论】:

          【解决方案4】:

          您必须实际找到文件路径,因为它不会将文件所在的目录推断为目标目录,这与 PC 上的 Python 不同。

          【讨论】:

            【解决方案5】:

            使用他们更改的内容。 您拥有的文件在 /accounts/mastercard root 内,而您在其中的文件是 /accounts/main.py 您只需要使用这个 /mastercard/xxxxx 喜欢我的代码

            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")
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2018-07-13
              • 2019-04-06
              • 1970-01-01
              • 2021-12-30
              • 1970-01-01
              • 2019-09-15
              相关资源
              最近更新 更多