Jump to content

In python how can I create a list in another .py file and write to it, also get its contents?

Wictorian

Using python I want to create another .py file. I know how to write and get thing to it. But how do I create a list and how do ı append to it. Also how do I get its contents?

Link to comment
Share on other sites

Link to post
Share on other sites

I don't really understand what you are asking for.

 

Your goal is to have one script create another python script, correct?

 

You already know how to write and retrieve data from a file. Correct?

 

You want to know how to create a list and how to append to it?

 

 

Here is a good guide on how to read and write lists to and from a file using Python.

https://stackabuse.com/reading-and-writing-lists-to-a-file-in-python/

Link to comment
Share on other sites

Link to post
Share on other sites

4 hours ago, LAwLz said:

I don't really understand what you are asking for.

 

Your goal is to have one script create another python script, correct?

 

You already know how to write and retrieve data from a file. Correct?

 

You want to know how to create a list and how to append to it?

 

 

Here is a good guide on how to read and write lists to and from a file using Python.

https://stackabuse.com/reading-and-writing-lists-to-a-file-in-python/

output:

€•        ]”Œsteam”a.€•       Œpath”.

 

I dont want anything other than "steam" and "path"

 

Link to comment
Share on other sites

Link to post
Share on other sites

What is your code to get that output? You're doing something wrong to get those weird characters when writing steam and path to the file.

Link to comment
Share on other sites

Link to post
Share on other sites

import pickle
apps = []
locations = []
fh = open("app locations.pkl", "wb")
apps.append(input())
locations.append(input())
print(apps)
pickle.dump(apps,fh)
print(locations)
pickle.dump(locations[0],fh)
fh.close()

This isnt from my the original source code of the app I am working on. 

I am experimenting with this right now.

@C2dan88

Link to comment
Share on other sites

Link to post
Share on other sites

For simple text stored in a file you should not need to be reading/writing files in binary mode

fh = open("app locations.pkl", "w")

Thats probably why you are getting the weird characters

Link to comment
Share on other sites

Link to post
Share on other sites

14 minutes ago, C2dan88 said:

For simple text stored in a file you should not need to be reading/writing files in binary mode


fh = open("app locations.pkl", "w")

Thats probably why you are getting the weird characters

now it gives this error

(I cant copy paste it for some reason so here is the screenshot)

temp.PNG

Link to comment
Share on other sites

Link to post
Share on other sites

Oh pickle writes in binary mode only. If you want the text file contents to be human readable then use json.

Link to comment
Share on other sites

Link to post
Share on other sites

43 minutes ago, C2dan88 said:

Oh pickle writes in binary mode only. If you want the text file contents to be human readable then use json.

I dont want it to be human readable but when I get the data will I just get steam and path or will I get those weird symbols

Link to comment
Share on other sites

Link to post
Share on other sites

6 hours ago, LAwLz said:

I don't really understand what you are asking for.

 

Your goal is to have one script create another python script, correct?

 

You already know how to write and retrieve data from a file. Correct?

 

You want to know how to create a list and how to append to it?

 

 

Here is a good guide on how to read and write lists to and from a file using Python.

https://stackabuse.com/reading-and-writing-lists-to-a-file-in-python/

oh you got me wrong. I dont wanna write the contents. I wanna create a list.

Link to comment
Share on other sites

Link to post
Share on other sites

1 hour ago, Wictorian said:

I dont want it to be human readable but when I get the data will I just get steam and path or will I get those weird symbols

Then you need to use pickle.load to turn the values stored in the file back into a variable

demo code, you can add app name and path. Save to file and list them from the file.

import pickle

apppath_file = "app locations.pkl"  # file to save to
apps = {}  # global variable for storing app path, note app name will be used as dictionary key


def add_app():
    app = input("App name: ")
    path = input("App location: ")
    apps.update({app: path})  # add values to app variable, as key value dictionary pairs


def save_apps():
    fh = open(apppath_file, "wb")
    pickle.dump(apps, fh)  # save contents of apps variable to file
    print("Saved apps to file")
    fh.close()


def load_apps():
    with open(apppath_file, 'rb') as f:
        apps = pickle.load(f)  # unpickle contents of file, restoring apps variable
    count = len(apps)
    print(f"There are {count} apps stored in '{apppath_file}'!\nWhich are:")
    # list app name => path values
    for app_name in apps:
        path = apps[app_name]  # use app_name as dictionary key to get path
        print(app_name + ' => ' + path)
    print('')


def main():
    choice = ''
    while choice != 'q':
        print("Type number option:")
        print("\t1 - add app")
        print("\t2 - save to file")
        print("\t3 - load from file")
        print("\tq - quit")

        choice = input("Option: ")
        print('')
        if choice == '1':
            add_app()
        elif choice == '2':
            save_apps()
        elif choice == '3':
            load_apps()


if __name__ == "__main__":
    main()

 

Link to comment
Share on other sites

Link to post
Share on other sites

18 hours ago, C2dan88 said:

Then you need to use pickle.load to turn the values stored in the file back into a variable

demo code, you can add app name and path. Save to file and list them from the file.


import pickle

apppath_file = "app locations.pkl"  # file to save to
apps = {}  # global variable for storing app path, note app name will be used as dictionary key


def add_app():
    app = input("App name: ")
    path = input("App location: ")
    apps.update({app: path})  # add values to app variable, as key value dictionary pairs


def save_apps():
    fh = open(apppath_file, "wb")
    pickle.dump(apps, fh)  # save contents of apps variable to file
    print("Saved apps to file")
    fh.close()


def load_apps():
    with open(apppath_file, 'rb') as f:
        apps = pickle.load(f)  # unpickle contents of file, restoring apps variable
    count = len(apps)
    print(f"There are {count} apps stored in '{apppath_file}'!\nWhich are:")
    # list app name => path values
    for app_name in apps:
        path = apps[app_name]  # use app_name as dictionary key to get path
        print(app_name + ' => ' + path)
    print('')


def main():
    choice = ''
    while choice != 'q':
        print("Type number option:")
        print("\t1 - add app")
        print("\t2 - save to file")
        print("\t3 - load from file")
        print("\tq - quit")

        choice = input("Option: ")
        print('')
        if choice == '1':
            add_app()
        elif choice == '2':
            save_apps()
        elif choice == '3':
            load_apps()


if __name__ == "__main__":
    main()

 

I actually have two lists, "apps" and "locations"

apps[0] has locations[0]

should I change that to what you did?

Link to comment
Share on other sites

Link to post
Share on other sites

Not sure why you want separate lists. My example stores them as key, value pairs.

 

If you want to get a list of app names use

names = list(apps)
print(names)

Similarly if you only want the paths use

paths = list(apps.values())
print(paths)

 

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×