Python To get the value of a variable that is entered in a function of a class in another file

Asked 6 days ago, Updated 6 days ago, 1 views

**file_a**
import os
import sys

class Jmembership():
    def new_name(self):
        path_input = r"C:/Users/Users/Desktop/Database/input/"

        join_name=input("Enter membership name :")

        if not (os.path.isdir(path_input + join_name)):
            os.makedirs(os.path.join(path_input + join_name))
            return join_name
        # # else:
        #     #     print("Error: Cannot create the directory {}".format(path_input + join_name)) 
        #     #     sys.exit()
**file_b**
import os

from file_a import Jmembership


class DataSP():
    def data_input(self, join_name):
        path_input = r"C:/Users/Users/Desktop/Database/input/"

        i = 0
        for name in os.listdir(path_input + join_name):
            src = os.path.join(path_input + join_name, name)
            dst = join_name + str(i) + '.png'
            dst = os.path.join(path_input + join_name, dst)
            os.rename(src, dst)
            i += 1


if __name__ == '__main__':
    jdb = Jmembership()
    jname = jdb.new_name()
    dsp = DataSP()
    dsp.data_input(jname)

I want to change the name of the file_a by receiving the data_input function of the file_b file and adding the image of the picture in the folder in number order. I don't think the name I've entered is transferred to file_b, what should I do?

python

2022-09-20 16:25

1 Answers

It is expected to be resolved by handing over the arguments as below.

class DataSP():
    def data_input(self, join_name):
        path_input = r"C:/Users/Users/Desktop/Database/input/"

        i = 0
        for name in os.listdir(path_input + join_name):
            src = os.path.join(path_input + join_name, name)
            dst = join_name + str(i) + '.png'
            dst = os.path.join(path_input + join_name, dst)
            os.rename(src, dst)
            i += 1


if __name__ == '__main__':
    jdb = Jmembership()
    jname = jdb.new_name()
    #dsp = DataSP()
    dsp = DataSP(jname)
    dsp.data_input(jname)


2022-09-20 16:25

If you have any answers or tips


© 2022 pinfo. All rights reserved.