Opening Text files in Qpthon 3 on android tablet

I am new to learning coding and starting with Python 2.7. How do I get a text file to open in Qpython 3?

# Opening a file

inp = raw_input ("Enter a File Name: ")

#Optional guardian to capture incorrect filenames
try:

fhand = open('words.txt')
except:
    print "Invalid Filename "
    exit()
#End of Guardian code

count = 0
for line in fhand:
    count = count + 1
print "Line Count", count

All I get from this is error messaging.


The error message IOError: [Errno 2] No such file or directory: 'words.txt' means that Python cannot find the file words.txt at the place where it's looking for it.

Where is Python looking for the file?

If the string passed to open() looks like an absolute path, Python will look there. 'words.txt' does not look like an absolute path, so it is treated as a relative path. Relative to what? Other than you might expect, not relative to your python source file, but relative to the current working directory .

If you invoked the script from an interactive shell, you probably know the applicable current working directory. I don't know if that's someone one can/does do when developing with Qpython. But if not, don't worry.

import os
print os.getcwd()

will output the current working directory's path, which should help you figure out where to put your file or how to access it.

Indentation matters in python

That the actual exception's message is output is good for debugging. But as you have an except block, it's probably not what you intended. If you want "Invalid Filename " to be output, you have to fix the indentation of the try block. ( fhand = open('words.txt') must be indented relative to try: .)

I'm actually a bit surprised this ran at all and you didn't get a message saying IndentationError: expected an indented block .

Proper exception handling

Only catch what you can (and do) handle correctly

Note that, once the try block is fixed (see section above), the except block will catch every exception in it, even if it isn't IOError: [Errno 2] No such file or directory . This means you'd output "Invalid Filename " even when there's a completely different problem in open , like running out of RAM to allocate.

So try to catch the specific error you will handle in the except block.

Use stderr for error messages

With

print >> sys.stderr, "Invalid Filename "

you can print to the standard error stream instead of the standard output stream. This will be useful if your script's standard output is redirected to a file.

Using the user input

You're currently using a hardcoded filename and doing nothing with the name the user has input when prompted Enter a File Name: . I guess once you're done debugging, you'll replace the hardcoded filename by imp , which here holds the user input. Be aware that by just substituting it, you will allow users to not just specify a file name, but a file path (absolute or relative to the current working dir) that your script will then access. This might or might not be something you want to permit.


There's no problem in qpython's path. The slash "/" could be found by "es file browser" And be changed and written after rooting. I v use "os.system('sh')" to get into andrio sonsole .type "python **.py" i can work,of course,you should put **.py into Divice and change its permission(commonly pretermit)

Es download: http://www.estrongs.com/?lang=en

Use follow to chang the path:(type in python console)

os.chdir("****")

**** should be /storage/emulated/0/py like this. You should type one by one,or it dont work,you know what i mean. I did it.

New::: when restat the console it gets back to the "/"..T_T


I think qpython has a problem here, I've been trying to figure it out myself but could be an unresolved qpython problem

print(os.getcwd()) only prints a single slash '/' for me so I have had to resolve to putting the full path in

rdir = "/mnt/sdcard/com.hipipal.qpyplus/projects3/fileio/ "
with open(rdir+"test.txt", "wt") as out_file:
    out_file.write("This text is going to _____________the out filen look at it and see")

You can read and write this way.

链接地址: http://www.djcxy.com/p/54716.html

上一篇: 在NLTK中找到语料库的路径

下一篇: 在Android平板电脑上打开Qpthon 3中的文本文件