0

Can a python script located in D:\Folder_A\Folder_B\be used to change variables in another python script located in D:\Folder_A\Folder_C\, and then print them out on the console?

NOTE: I'm a beginner in Python, and I'm using v2.7.8.

EDIT: To answer @Rik Verbeek, I created a python file in location A (D:\Folder_A\Folder_B\), and another file at location B (D:\Folder_A\Folder_C\), with both the folders (Folder_B & Folder_C) located in D:\Folder_A\. In the 2nd .py file, I wrote the following:

a = 0 b = 0

Now I want to change these variables from the 1st .py file to 5 and 10 respectively, and print them to the console. Also, these files are not in the Python libraries, but are instead, located in another folder(Folder_A).

To answer @Kasra (and maybe @cdarke), I don't want to import a module, unless it is the only way.

pycrazy
  • 3
  • 2
  • Be a bit more specific about what you want. Do you want to adjust the script in the folder? Or does your program need to change variables runtime? Also post a bit of code so we know what you created so far. – Rik Verbeek Nov 18 '14 at 12:45
  • possible duplicate of [How to import a module given the full path?](http://stackoverflow.com/questions/67631/how-to-import-a-module-given-the-full-path) – Mazdak Nov 18 '14 at 12:45
  • Only if you `import` it as a module, not if you run it as a different process (unless you use some form of Inter Process Communication). – cdarke Nov 18 '14 at 12:48
  • So basically you want to open the file, search for the variables, change the values and safe the file? – Rik Verbeek Nov 18 '14 at 13:23
  • if you want to edit the files or use command `import os`, if not import as a module and make sure it is in your python path – jgr208 Nov 18 '14 at 13:28
  • yse @RikVerbeek, but I also want to print them out on the console of the running script of the 1st .py file. (I know "r+" can do this, but then what will be the code) – pycrazy Nov 18 '14 at 13:44

1 Answers1

0

If you have some "global" variables I think is a good idea to have them in a separated module and import that module from each place you need them. This way you only have to do it as cdarke has commented.

pypy
  • 443
  • 5
  • 19
  • so you are saying to make another file, add these variables, and import them wherever I need them? – pycrazy Nov 18 '14 at 13:45
  • Yes, I like this way because you have a module to this explicitly purpose (global variables). – pypy Nov 19 '14 at 15:00