Python Forum
How to write variable in a python file then import it in another python file?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to write variable in a python file then import it in another python file?
#1
How to write variable in a python file then import it in another python file?
hex_mova64w0c0 = "00008052"

hex_mova64w1c0 = "01008052"
hex_mova64w2c0 = "02008052"
...
hex_mova64w30c0 = "1E008052"
hex_mova64w31c0 = "1F008052"

hex_suba64w0w0w0 = "0000004B"
hex_suba64w0w1w1 = "2000014B"
...
hex_suba64w0w30w30 = "C0031E4B"
hex_suba64w0w31w31 = "E0031F4B"
Then I tried to import it in another python file:
import re

import _Arm64HexDatabase


handle_file1 = open("example1", "rb")

hex_read = handle_file1.read()

handle_file1.close()


hex_read = re.sub(hex_suba64w0w0w0, hex_mova64w0c0, hex_read)
hex_read = re.sub(hex_suba64w0w1w1, hex_mova64w0c0, hex_read)
...
hex_read = re.sub(hex_suba64w0w30w30, hex_mova64w0c0, hex_read)
hex_read = re.sub(hex_suba64w0w31w31, hex_mova64w0c0, hex_read)
but I thought the import of my another file is failed

How to do that?
Reply
#2
Which one is better?

from _Arm64HexDatabase import *

or


import _Arm64HexDatabase as <short_name>

?
Reply
#3
Hi,

you can import basically any object defined on the top level from another file and use it.

Simple example, assuming both files are in the same directory:

File foo.py:

greeting = 'Hello World'
the_answer = 42
File bar.py:

import foo
print(foo.greeting)
print(f'The answer is: {foo.the_answer}')

#import a specific value
from foo import the_answer
not_an_answer = the_answer - 1
print(f'{not_an_answer} is not an answer')
Regards, noisefloor
Pedroski55 likes this post
Reply
#4
You are doing a lot of unserray job🔨,can you use a loop in both script.

hex_mova64w: Generates 00008052, 01008052, ..., 1F008052 for indices 0-31.
hex_suba64w: Generates 0000004B, 2000014B, ..., E0031F4B for indices 0-31
# _Arm64HexDatabase.py
hex_mova64w = [
    f"{i:02X}008052" for i in range(32)
]

hex_suba64w = [
    f"{i:02X}00{i:02X}4B" for i in range(32)
]
Use bytes.fromhex() to convert the hex strings into binary format for processing.
As i mention in your other Thread
Then no need for regex and use loop to iterate through all possible indices.
from _Arm64HexDatabase import hex_mova64w, hex_suba64w

with open("example1", "rb") as handle_file1:
    hex_read = handle_file1.read()

# Perform substitutions in loop
for i in range(32):
    pattern = bytes.fromhex(hex_suba64w[i])
    replacement = bytes.fromhex(hex_mova64w[0])
    hex_read = hex_read.replace(pattern, replacement)

with open("example1_modified", "wb") as handle_file2:
    handle_file2.write(hex_read)
Reply
#5
files are just a sequence of characters. you can think of it as one big string. you can break it up into many strings by some rule you choose. a commonly chosen rule is to break a line at each ASCII newline character, usually (but not always) also discarding that newline character.

what do you expect to first be in these files?

when you say "python file", what do you mean? a file of code in the Python language? when you want to "import a variable" into it, do you intend for the variable to remain in that file after this is successfully done?
Gribouillis likes this post
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  I trying to automate the Variable Logon button using the python code but I couldn't surendrasamudrala 0 255 Mar-07-2025, 05:02 AM
Last Post: surendrasamudrala
  How can I write formatted (i.e. bold, italic, change font size, etc.) text to a file? JohnJSal 12 27,870 Feb-13-2025, 04:48 AM
Last Post: tomhansky
  How to remove unwanted images and tables from a Word file using Python? rownong 2 714 Feb-04-2025, 08:30 AM
Last Post: Pedroski55
  Best way to feed python script of a file absolut 6 1,047 Jan-11-2025, 07:03 AM
Last Post: Gribouillis
  Removal of watermark logo pdf file Python druva 0 646 Jan-01-2025, 11:55 AM
Last Post: druva
  How to communicate between scripts in python via shared file? daiboonchu 4 1,512 Dec-31-2024, 01:56 PM
Last Post: Pedroski55
  Problems writing a large text file in python Vilius 4 956 Dec-21-2024, 09:20 AM
Last Post: Pedroski55
  python 3.13 : import whois HansieB 1 610 Nov-30-2024, 02:58 PM
Last Post: snippsat
  How to re-register .py file extension to new moved Python dir (on Windows)? pstein 5 1,246 Nov-06-2024, 03:06 PM
Last Post: DeaD_EyE
  [SOLVED] [Linux] Write file and change owner? Winfried 6 1,487 Oct-17-2024, 01:15 AM
Last Post: Winfried

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020