-1

I am running probes on a number of campus devices daily and log the Probe for each day to a file that takes the name: HOST-YYMMDD.txt (e.g. "Host1-Loc1-171219.txt")

I am trying to automate the generation of variables that would allow xlsxwriter to 'read' the correct files for the loops that I need to run afterwards.

H1L1 = "Host1-Loc1"
H1L2 = "Host2-Loc2"

Dates = [171219,171220,171221]
Hosts = [H1L1,H1L2]

I am trying to create variables using the name pattern "Probe[Date]" (e.g. "Probe171219", "Probe171220" etc.) that are equal to (respectively) the log files "Host1-Loc1-171219.txt" and "Host1-Loc1-171220.txt"

I have been able to print the value that I need to get for the Probe variables, but I've not managed to assign it to an actual variable in an automatic way.

for item in Dates:
   print('FileLocation',Hosts[0],'-',item,'.txt',sep='')

This prints:

FileLocation/Host1-Loc1-191219.txt
FileLocation/Host1-Loc1-191220.txt

The question is how do I assign this to variables that would be called "Probe171219" and "Probe171220", thus giving the result:

Probe171219 = FileLocation/Host1-Loc1-191219.txt
Probe171220 = FileLocation/Host1-Loc1-191220.txt

This far I've been populating my variables manually, but with over 50 new logs per day, it is not very salable.

Asenski
  • 49
  • 1
  • 1
  • 8
  • 3
    Have you considered using a dictionary? – pault Jan 16 '18 at 15:27
  • I did as per Rakesh'es example below (I am new to Python and did not know how about dictionaries). It works but defines variables with names such as 'Probe171219]' which makes them unusable further on due to the ']' in the variable name. – Asenski Jan 17 '18 at 09:26

1 Answers1

1

As pault mentioned you can use a dictionary:

Sample:

Dates = [171219,171220,171221]
Hosts = ["H1L1","H1L2"]
d = {}
for i in zip(Dates,Hosts):
    d["Probe{0}".format(i[0])] = "FileLocation/{0}.txt".format(i[1])

print d

Result:

{'Probe171219': 'FileLocation/H1L1.txt', 'Probe171220': 'FileLocation/H1L2.txt'}
Rakesh
  • 81,458
  • 17
  • 76
  • 113
  • Thank you for the pieces of advice, Rakesh! The end result however gives me variables with a "]" in their name (e.g. 'Proble171219':'FileLocation/H1L1-171219.txt' that makes them impossible to manipulate further: e.g. if I do "open (Probe171219) as P1219:" Python comes back with the information that such variable is not defined. The variable "Probe171219]" cannot be open because of the "]" in the variable name. I am not certain how to go about this issue with the naming. – Asenski Jan 17 '18 at 09:18
  • I dnt understand where are you getting "]"? – Rakesh Jan 17 '18 at 09:43
  • The "]" appears in the variable names that Python prints out. When I do a print(d) the output is: RESTART: /Scout.py {'Probe171219]': 'FileLocation/H1L1-171219.txt', ... }". So basically it appears in the dictionary entry that I wish to use as a variable for later manipulation. I managed to overcome the problem all together by auto-populating the log names in a list though. I did Host_Probes = [] /n LogDoc = 'FileLocation' /n/ for root, dirs, files in oswalk (LogDoc): /n for name in files: /n if H1L1 in name: /n Host_Probes.append(str((os.path.join(name[:18])))) and it works now. – Asenski Jan 17 '18 at 10:37