0

I have a script (let's call it script 1) that, when run, requests a name to be typed (only one name can be entered at a time, so you have to run the script again to add another name)

name = input('\n Please enter your name:')

In another script (let's call it script 2), I have to manually type these names in order

names = ['None', 'Name1', 'Name2', 'Name3']

This is line 13 of 54 in script 2

Is it possible to automatically add the name(s) typed in script 1 (after running script 1) to the names list in line 13 of script 2? For example, say I run script 1 and when it prompts me to enter a name I type in the name John. How do I have the script automatically add " , 'John' " (without " ") to the names list in script 2?

  • 1
    Do you really want script1 to modify script2? That's usually a very bad idea. Better solutions include integrating the two together so script1 imports script2 as a module and passes the value into some function, or storing the data in a config file instead of hardcoded in the source. – abarnert Mar 17 '18 at 22:52
  • I think what you _probably_ want here is to use a simple textfile (one name per line) tha script1 appends to and script2 reads, and I can write an answer showing how to do that if you give us [a stripped-down version of script1 and script edited into the question](https://stackoverflow.com/help/mcve). If that's not what you want, maybe explain why. – abarnert Mar 17 '18 at 22:59
  • Thanks for the text file suggestion, it works great. My issue now is that I don't know how to get the 'names' list in script 2 to build its list using the names in the text file. Would [this](https://stackoverflow.com/a/328068/9510055) work? – Pergamon256 Mar 17 '18 at 23:34
  • Yes—although `with open(filename) as f: lines = list(f)` is a little better. (That `with` makes sure you close the file immediately, instead of leaving it hanging around open.) – abarnert Mar 17 '18 at 23:36

1 Answers1

0

If you really do want to edit the source of script2, you can do that—via normal string methods, or regex, or, better, tokenizing or parsing the file. But really, you almost certainly don't want to do that.

If you want some kind of persistent storage across runs of your program, you probably want a separate file. For fancy cases, you may want to use a database like dbm or sqlite3, but here, you just have a list of names, so you can probably store them one per line in a simple text file.

There are two tricky bits, but both are pretty easy:

  • If you've never run script1, there won't be a file yet. So you'll want to handle that case.
  • You need to make sure to write newlines ('\n') after each name, and then strip them back off on the other end.

script1.py:

name = input('\n Please enter your name:')
with open('names.txt', 'w') as f:
    f.write(name + '\n')

script2.py:

try:
    with open('names.txt') as f:
        names = [line.rstrip() for line in f]
except FileNotFoundError:
    names = []

If you wanted to have a "starting list" of names that worked even before ever calling script1, you'd add a names = ['Name1', 'Name2'] line about theopen, and the change thenames = […]tonames.extend(…)`. But it sounds like you don't want that, so I've made it start with an empty list—the only names are the ones you added to the text file.

One more thing: This look for names.txt in the current working directory. So if you run your script from some other directory, it won't find the file. If you want to find it in a specific location instead, like /Users/Pergamon256/.config/my-app/names.txt, that's pretty easy. If you want "wherever is the normal place for my operating system", that's a bit trickier; there are libraries on PyPI that do it for you. If you want the same directory that the scripts are in, there are good answers on SO showing how to do that.

abarnert
  • 354,177
  • 51
  • 601
  • 671
  • But how do I get it so that I don't have to manually make the list of names? I want the list to pull names line by line from the text file and use them to make the list. I'd prefer not to have to type in Name1, Name2, Name3, Name4, etc. and manually update the list every time. – Pergamon256 Mar 17 '18 at 23:59
  • @Pergamon256 I thought you wanted to have a "starting list" even before the first time you ran script1. If you don't want that, I'll edit the answer. – abarnert Mar 18 '18 at 00:00
  • Yeah, I would prefer that the list is entirely dependent on the names in the text file and not require any to be manually typed in script 2 – Pergamon256 Mar 18 '18 at 00:02