How can I call a variable from another file? - python

I have two files. The first file, we'll call it "Main.py". The second, "file1.py".
I want to be call a variable from Main.py and write the value to a new file called "tempFile.txt". I've tried importing "Main.py" but I get an Attritbute error.
Here's an example of "File1.py"
import os
import sys
import main
# This function should write the values from Main.py to a tempFile
# and reads the contents to store into a list.
def writeValues():
tempFile = open('tempFile.txt', 'w+')
tempFile.write(str(X_Value))
tempFile.write("\n")
tempFile.write(str(Y_Value))
zoneValues = [line.rstrip('\n') for line in open('tempFile.txt')]
print zoneValues
# X_Value and Y_Value are the variables in Main.py I am trying to access
def readZoneValues(): # Creates a list from values in tempFile.txt
valuesList = [line.rstrip('\n') for line in open('tempFile.txt')]
print valuesList
I've tried other looking for answers but there was no clear answer to this specific issue.
EDIT:
Main.py
import os
import sys
import file1
X_Value = 1000
Y_Value = 1000
# For statement that manipulates the values, too long to post.
for "something":
if "something":
# after the values are calculated, kick it to the console
print "X Value: " + str(X_Value) + "\n"
print "Y Value: " + str(Y_Value) + "\n"
I need the values of the variables to be written to the tempFile after Main.py has been processed.
EDIT:
I have tried having the tempFile created in Main.py, but for some reason my function for reading the tempFile and adding the values to a list do not appear, however, the values DO APPEAR after I delete the tempFile creation in Main.py and uncomment the write function in File1.py

The code you're presenting creates a circular import; i.e. main.py imports file1.py and file1.py imports main.py. That doesn't work. I would recommend changing write_values() to accept two parameters, and then passing them in from main.py, and eliminating the import of main into file1:
main.py:
import os
import sys
import file1
X_Value = 1000
Y_Value = 1000
file1.writeValues(X_Value, Y_Value)
file1.py:
import os
import sys
# This function should write the values from Main.py to a tempFile
# and reads the contents to store into a list.
def writeValues(X_Value, Y_Value):
tempFile = open('tempFile.txt', 'w+')
tempFile.write(str(X_Value))
tempFile.write("\n")
tempFile.write(str(Y_Value))
tempFile.close()
zoneValues = [line.rstrip('\n') for line in open('tempBeds.txt')]
print zoneValues
def readZoneValues(): # Creates a list from values in tempFile.txt
valuesList = [line.rstrip('\n') for line in open('tempFile.txt')]
print valuesList

Try importing it.
from YourFileName import *
Also, when calling,
YourFileName.tempFile
If you want to call only your variable then,
from YourFileName import VarName1

Related

Python: Look for Files that will constantly change name

So, I'll explain briefly my idea, then, what I've tried and errors that I've got so far.
I want to make a Python script that will:
Search for files in a directory, example: /home/mystuff/logs
If he found it, he will execute a command like print('Errors found'), and then stop.
If not, he will keep it executing on and on.
But other logs will be there, so, my intention is to make Python read logs in /home/mystuff/logs filtering by the current date/time only.. since I want it to be executed every 2 minutes.
Here is my code:
import time
import os
from time import sleep
infile = r"/home/mystuff/logs`date +%Y-%m-%d`*"
keep_phrases = ["Error",
"Lost Connection"]
while True:
with open(infile) as f:
f = f.readlines()
if phrase in f:
cmd = ['#print something']
erro = 1
else:
sleep(1)
I've searched for few regex cases for current date, but nothing related to files that will keep changing names according by the date/time.. do you have any ideas?
You can't use shell features like command substitutions in file names. To the OS, and to Python, a file name is just a string. But you can easily create a string which contains the current date and time.
from datetime import datetime
infile = r"/home/mystuff/logs%s" % datetime.now().strftime('%Y-%m-%d')
(The raw string doesn't do anything useful, because the string doesn't contain any backslashes. But it's harmless, so I left it in.)
You also can't open a wildcard; but you can expand it to a list of actual file names with glob.glob(), and loop over the result.
from glob import glob
for file in glob(infile + '*'):
with open(file, 'r') as f:
# ...
If you are using a while True: loop you need to calculate today's date inside the loop; otherwise you will be perpetually checking for files from the time when the script was started.
In summary, your changed script could look something like this. I have changed the infile variable name here because it isn't actually a file or a file name, and fixed a few other errors in your code.
# Unused imports
# import time
# import os
from datetime import datetime
from glob import glob
from time import sleep
keep_phrases = ["Error",
"Lost Connection"]
while True:
pattern = "/home/mystuff/logs%s*" % datetime.now().strftime('%Y-%m-%d')
for file in glob(pattern):
with open(file) as f:
for line in f:
if any(phrase in line for phrase in keep_phrases):
cmd = ['#print something']
erro = 1
break
sleep(120)

global name 'json' is not defined

Here is the beginning of createPeliMelo.py
def creation(path,session):
myPathFile=path+session+'.txt'
print myPathFile
pelimeloFile = open(path+session+'.txt', 'r')
with pelimeloFile as inf:
data = json.loads(inf.read())
Here is my Python script inside Maya:
import maya.cmds as cmds
import json
import os
from itertools import islice
import createPeliMelo as PeliMelo
PeliMelo.creation('C:/Users/francesco/Desktop/pelimelo video printemps/','session5723')
Here is the error I got:
Error: line 1: NameError: file C:/Users/francesco/Documents/maya/2016/scripts\createPeliMelo.py line
17: global name 'json' is not defined #
Line 17 is: data = json.loads(inf.read())
Where am I wrong?
When you import something, that import only applies to the file that you imported it in. This means that if you want to use json in createPeliMelo.py you need to do import json in THAT file, not your second script. Imports from one file will not propagate over to another.

How can I make python script(X) reload dynamically changing variables in another module(Y) and then re-import updated module(Y) in same script(X)?

I'm new to Python, I'm stuck with a code. I have tried my best to show my problem with below sample code. I'm playing with 4 files.
This is the runme file. That I'm running.
command > python runme.py
import os
with open("schooldata.txt", "r") as filestream: #opening schooldata.txt file
for line in filestream:
currentline = line.split(",")
a = currentline[0]
b = currentline[1]
c = currentline[2]
#creating a school_info.py file with value of a,b,c that are further imported by mainfile.py
f = open('school_info.py','w')
f.write("a= \"" + currentline[1] + "\"\n")
f.write("b= \"" + currentline[2] + "\"\n")
f.write("c= \"" + currentline[3] + "\"\n")
f.close()
#importing mainfile.py and calling its functions.
from mainfile import give_to_student
give_to_student("Rickon")
from mainfile import give_to_teacher
give_to_student("Carolina")
Second file is schooldata.txt from where I want to read the value of a,b,c. This is our main school data file from which we take authorization data. I'm reading line by line from this file and creating a,b,c by splitting it with (,).
12313,mshd1732,2718230efd,
fhwfw,382842324,238423049234230,
fesj32,282342rnfewk,43094309432,
fskkfns,48r209420fjwkfwk,2932042fsdfs,
38234290,fsfjskfjsdf,2942094929423,
Third file is school_info.py which I'm creating with this data everytime. This file is created everytime when a line is read from schooldata.txt file. So fresh file everytime with fresh and unique data of a,b,c.
a = "asb12"
b = "121002"
c = "mya122344"
Now here comes the mainfile.py which is having functions like give_to_student and give_to_teacher. This file is importing data from school_info.py, so as to create authorization code using values of a,b,c.
and function definition of give_to_student and give_to_teacher which uses these function definitions.
import os
import schoollib #(internal school lib)
#importing School_info.py file so as to get value of a,b,c
from school_info import *
#It creates authorisation code internally
lock = auth(a,b,c,d)
#This authorisation code is used to call internal function
def give_to_student():
lock.give(student)
def give_to_teacher():
lock.give(teacher)
So now let me share the exact problem that I'm facing as of now, I'm unable to get authorization code loaded for mainfile.py everytime it is imported in runme.py file. When I'm calling runme.py file it is giving same authorization code to all users every time.
It is not able to use authorization code that is create after reading second line of schooldata.txt
With mainfile.py file If I'm trying to reload module using. import importlib and then importlib.reload(mainfile.py) in runme.py.
#Added it in runme.py file
import importlib
importlib.reload(mainfile)
It is still giving authorization for first line of data(schooldata.txt).
Similar thing I tried in mainfile.py.
I tried to import importlib and then importlib.reload(school_info).
#added it in mainfile.py
import importlib
importlib.reload(school_info)
importlib.reload(school_info)
NameError: name 'school_info' is not defined
But it giving error, that school_info module doesn't exist.
Please throw some light on it, and how can I make it work.
P.S. I'm using python 3.5. Thanks
Why don't you try to combine the school_info.py and mainfile.py.
If you can run a combined loop.
import os
import schoollib #(internal school lib)
with open("schooldata.txt", "r") as filestream: #opening schooldata.txt file
for line in filestream:
currentline = line.split(",")
a = currentline[0]
b = currentline[1]
c = currentline[2]
#It creates authorisation code internally
lock = auth(a,b,c,d)
#This authorisation code is used to call internal function
def give_to_student():
lock.give(student)
def give_to_teacher():
lock.give(teacher)
#function calling
give_to_student("Rickon")
give_to_student("Carolina")
I hope this solves your purpose.

Call Python class methods from the command line

so I wrote some class in a Python script like:
#!/usr/bin/python
import sys
import csv
filepath = sys.argv[1]
class test(object):
def __init__(self, filepath):
self.filepath = filepath
def method(self):
list = []
with open(self.filepath, "r") as table:
reader = csv.reader(table, delimiter="\t")
for line in reader:
list.append[line]
If I call this script from the command line, how am I able to call method?
so usually I enter: $ python test.py test_file
Now I just need to know how to access the class function called "method".
You'd create an instance of the class, then call the method:
test_instance = test(filepath)
test_instance.method()
Note that in Python you don't have to create classes just to run code. You could just use a simple function here:
import sys
import csv
def read_csv(filepath):
list = []
with open(self.filepath, "r") as table:
reader = csv.reader(table, delimiter="\t")
for line in reader:
list.append[line]
if __name__ == '__main__':
read_csv(sys.argv[1])
where I moved the function call to a __main__ guard so that you can also use the script as a module and import the read_csv() function for use elsewhere.
Open Python interpreter from the command line.
$ python
Import your python code module, make a class instance and call the method.
>>> import test
>>> instance = test(test_file)
>>> instance.method()

Reading specific lines from a file and writing to another file in python

I have a file containing 1000 lines. First 15 lines will be header information.
I am trying to do the below :-
1) Read the file
2)Get the number of lines with header information. It will return 15
3)write lines 1-15 to a text file.
I am able to do 1 and 2 correctly, but not the 3rd step. Any inputs please?
Below is my code
#!/usr/bin/python
import numpy;
import os;
import math;
import cPickle; import time
from numpy import arange, sign
import copy
import re
import sys
import string
import mmap
head_lines = 0;
count=1
fide = open("text1.txt","r");
while (count==1): #We skip header
head_lines = head_lines+1;
line = fide.readline();
if 'END OF HEADER' in line:
print 'End of the Header reached'
break
print "hello world"
print head_lines
nlines = head_lines;
key=1;
while (key < nlines):
file1 = open("Header.txt","w")
lines = fide.readline()
file1.write(lines)
key = key+1;
print "done"
with open("input.txt") as f1:
with open("output.txt","w") as f2:
for _ in range(15):
f2.write(f1.readline())
is that what you are asking for?
(in python2.7 I think you can do with open('f1') as f1,open('f2','w') as f2: ...)
There are two problems in your code:
What ever you are writing in the file header.txt you'll be overwriting it every time you loop on the second while because you are re-opening the file which will put the file pointer to its origin i.e. the start of the file.
The second issue is similar but on the other file fide. You open it putting the file pointer to the start of the file and read one line until the end of the headers. And in the second while loop you keep reading lines from the same file pointer fide so you're reading the next nlines.
You could store the header's lines in a list and then write those strings in the output file.

Categories

Resources