Opening a file | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Opening a file

How do you open and read or modify the contexts of a file? I get errors only! And i use pydroid 3 compiler for android Myfile = open('ibrahim.txt') Output: Traceback (most recent call last): File "/data/user/0/ru.iiec.pydroid3/files/accomp_files/iiec_run/iiec_run.py", line 31, in <module> start(fakepyfile,mainpyfile) File "/data/user/0/ru.iiec.pydroid3/files/accomp_files/iiec_run/iiec_run.py", line 30, in start exec(open(mainpyfile).read(), __main__.__dict__) File "<string>", line 1, in <module> FileNotFoundError: [Errno 2] No such file or directory: 'ibrahim.txt' [Program finished]

23rd Mar 2020, 1:32 AM
Abdulwahab
Abdulwahab - avatar
14 Answers
+ 2
You need to share your code within thread Description, no one knows what's wrong until a code is here for a review. (Edit) Review the "Exceptions and Files" chapter https://www.sololearn.com/learn/JUMP_LINK__&&__Python__&&__JUMP_LINK/2445/ Follow the guide below to share your code 👍 https://www.sololearn.com/post/75089/?ref=app
23rd Mar 2020, 2:00 AM
Ipang
+ 1
Just great. Now in order to use the file i have to rewrite the contents. f= open("ibrahim.txt","w") cont= f.write('ibrahim is a programming geek') f=open("ibrahim.txt","r") print (f.read()) f.close Output: ibrahim is a programming geek [Program finished]
23rd Mar 2020, 3:31 AM
Abdulwahab
Abdulwahab - avatar
+ 1
https://developer.android.com/training/data-storage?hl=de
23rd Mar 2020, 5:17 AM
Oma Falk
Oma Falk - avatar
0
Abdulwahab The error message said that the file "ibrahim.txt" doesn't exist. Make sure a file exists before using it. I would suggest to review "Exceptions and Files" chapter for further details 👍
23rd Mar 2020, 3:22 AM
Ipang
0
Ipang, The file exists Internal shared storage\\ibrahim.txt
23rd Mar 2020, 3:28 AM
Abdulwahab
Abdulwahab - avatar
0
Is the Python code saved in the same folder with that of the "ibrahim.txt"? you only specify the filename, no path. By that, it searches the same folder for the file.
23rd Mar 2020, 3:34 AM
Ipang
0
No it isn't At first, i tried running the code with the file saved in the same folder with the code and it still didn't work
23rd Mar 2020, 3:36 AM
Abdulwahab
Abdulwahab - avatar
0
Not sure, but maybe because you didn't specify file access in the beginning, only the filename. I'm staying to hear what others has to say ...
23rd Mar 2020, 3:41 AM
Ipang
0
All right, now it is and still nothing
23rd Mar 2020, 3:41 AM
Abdulwahab
Abdulwahab - avatar
0
If you are using pyroid.. Then first please create a new folder then save "<filename>.txt" file in it...then create a new .py file in which you do whatever you want to do (like reading a file and print it) then save the file in same folder and then run it...
23rd Mar 2020, 5:44 PM
ANJALI SAHU
0
Ok RAJESH SAHU i took your advice and: / $ / $ cd '/storage/emulated/0/Abdulwahab's python projects' ; export PYTHONPATH='/storage/emulated/0/Abdulwahab's python projects' ; export PKG_CONFIG_PATH='/data/user/0/ru.iiec.pydroid3/files/aarch64-linux-android/lib/pkgconfig';export SSL_CERT_FILE='/data/user/0/ru.iiec.pydroid3/files/cert.pem';export TERMINFO='/data/user/0/ru.iiec.pydroid3/files/aarch64-linux-android/share/terminfo';export LD_LIBRARY_PATH='/data/user/0/ru.iiec.pydroid3/files/aarch64-linux-android/lib';export TMPDIR='/data/user/0/ru.iiec.pydroid3/cache';export SHELL='/data/user/0/ru.iiec.pydroid3/files/sh';export CONFIG_SHELL='/data/user/0/ru.iiec.pydroid3/files/sh';export LIBRARY_PATH='/data/user/0/ru.iiec.pydroid3/files/lib/gcc/aarch64-linux-android/8.2.0/';export XDG_CACHE_HOME='/data/user/0/ru.iiec.pydroid3/cache';export APP_PACKAGE='/data/app/ru.iiec.pydroid3-q24XMYtoUpmwCORrOzd-4g``/base.apk';export LC_ALL='en_US.UTF-8';export JOBLIB_MULTIPROCESSING='0';export KERAS_BACKEND='theano';export PYD
23rd Mar 2020, 9:12 PM
Abdulwahab
Abdulwahab - avatar
0
You know what, I'll just stick to rewriting the contents of the file before using it cause otherwise, this sucks!
23rd Mar 2020, 9:16 PM
Abdulwahab
Abdulwahab - avatar
0
My text file have 1500 words but it only gives info details about the text file. Not reading it. ====My code===== file = open("/storage/emulated/0/Download/newocr.com-20200628165538.txt", "r") print(file) orig=file.readlines()[1:] file.close() print(orig) ====Output===== <_io.TextIOWrapper name='/storage/emulated/0/Download/newocr.com-20200628165538.txt' mode='r' encoding='UTF-8'> [] [Program finished]
28th Jun 2020, 5:39 PM
RA DSO Gandhinagar
RA DSO Gandhinagar - avatar