Python Message ResourceWarning: unclosed file

Hi all,

Repost from github.

I got an error message when running the Python code for Python versions 3.10 and 3.15 on GitHub when script1.py was executed. When I run the Termux OS Python 3.12, i did not have any issues.

I try tracemalloc but I am a beginner. Anybody can help or pointers in order to troubleshoot the error ?

The details are below.

Python code

exec(open('script1.py').read())

The Error message

<stdin>:1: ResourceWarning: unclosed file <_io.TextIOWrapper name='script1.py' mode='r' encoding='UTF-8'>
ResourceWarning: Enable tracemalloc to get the object allocation traceback

Python 3.10

~/ $ python3.10d
Python 3.10.19+ (heads/3.10:2f84024, Jan 21 2026, 09:33:01) [Clang 21.1.8 ] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> exec(open('script1.py').read())
<stdin>:1: ResourceWarning: unclosed file <_io.TextIOWrapper name='script1.py' mode='r' encoding='UTF-8'>
ResourceWarning: Enable tracemalloc to get the object allocation traceback
linux
64
What is thisWhat is thisWhat is thisWhat is thisWhat is this

Python3.12

~/ $ python3.12d
Python 3.12.12 (main, Jan 18 2026, 04:37:25) [Clang 21.0.0 (https://android.googlesource.com/toolchain/llvm-project 5e96669f0 on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> exec(open('script1.py').read())
linux
64
What is thisWhat is thisWhat is thisWhat is thisWhat is this
>>>

Python 3.15

~/ $ python3.15d
Python 3.15.0a5+ (heads/main:48795b64, Jan 21 2026, 11:46:27) [Clang 21.1.8 ] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> exec(open('script1.py').read())
<python-input-0>:1: ResourceWarning: unclosed file <_io.TextIOWrapper name='script1.py' mode='r' encoding='utf-8'>
ResourceWarning: Enable tracemalloc to get the object allocation traceback
linux
64
What is thisWhat is thisWhat is thisWhat is thisWhat is this
>>>

Termux info.

~ $ termux-info
Termux Variables:
TERMUX_APK_RELEASE=GITHUB
TERMUX_APP_PACKAGE_MANAGER=apt
TERMUX_APP_PID=11710
TERMUX_APP__DATA_DIR=/data/user/0/com.termux
TERMUX_APP__LEGACY_DATA_DIR=/data/data/com.termux
TERMUX_APP__SE_FILE_CONTEXT=u:object_r:app_data_file:s0:c55,c257,c512,c768
TERMUX_APP__SE_INFO=default:targetSdkVersion=28:complete
TERMUX_IS_DEBUGGABLE_BUILD=1
TERMUX_MAIN_PACKAGE_FORMAT=debian
TERMUX_VERSION=0.118.3
TERMUX__HOME=/data/data/com.termux/files/home
TERMUX__PREFIX=/data/data/com.termux/files/usr
TERMUX__ROOTFS_DIR=/data/data/com.termux/files
TERMUX__SE_PROCESS_CONTEXT=u:r:untrusted_app_27:s0:c55,c257,c512,c768
TERMUX__USER_ID=0
Packages CPU architecture:
aarch64
Subscribed repositories:
# sources.list
deb https://grimler.se/termux/termux-main stable main
# root-repo (sources.list.d/root.list)
deb https://grimler.se/termux/termux-root root stable
# x11-repo (sources.list.d/x11.list)
deb https://grimler.se/termux/termux-x11 x11 main
Updatable packages:
cmake/stable 4.2.2 aarch64 [upgradable from: 4.2.1]
openjdk-25-x/stable 25.0.2 aarch64 [upgradable from: 25.0.1-1]
openjdk-25/stable 25.0.2 aarch64 [upgradable from: 25.0.1-1]
termux-tools version:
1.45.0
Android version:
14
Kernel build information:
Linux localhost 5.4.289-qgki-ga319a11388f1 #1 SMP PREEMPT Mon Oct 20 14:47:03 UTC 2025 aarch64 Android
Device manufacturer:
Xiaomi
Device model:
22111317G
Supported ABIs:
SUPPORTED_ABIS: arm64-v8a,armeabi-v7a,armeabi
SUPPORTED_32_BIT_ABIS: armeabi-v7a,armeabi
SUPPORTED_64_BIT_ABIS: arm64-v8a
LD Variables:
LD_LIBRARY_PATH=
LD_PRELOAD=/data/data/com.termux/files/usr/lib/libtermux-exec-ld-preload.so

script.py

# A first python script
import sys
print (sys.platform)
print (2 ** 6)
x = 'What is this'
print(x * 5)

open(file_name) returns an object that should be closed. You’re getting a warning, not an error, because it’s a ‘should’ not a ‘must’.

The classic way to use open is as

with open(file_name) as file:
  text = file.read()
exec(text)

using a with block ensures the file is closed when the with block ends.

You can also use

from pathlib import Path

exec(Path(file_name).read_text())

pathlib.Path.read_text() handles opening and closing the file for you, so that you don’t need a with statement.

Finally, I would strongly recommend against the whole “read text from file then exec” construction. You can run from the command line

python3.15d file_name

or

python file_name

(Or whatever other alias you have set up)

and from within Python you can run the file most robustly by importing it. That does require setting up an appropriate folder structure and creating an __init__.py file, but then you can run

import my_file

and that will run the file content for you.

2 Likes