Python Append Txt.file

Viewed 25

I'm working on a python script that I will get all inactive users and list it on a txt file. I have append it on a txt.file however it seems "\n" does not give me proper results that I need.

This is my code.

with open('InactiveUsersList', 'w') as f:
                    inactiveUser = (str(userDetail.get("firstName")) + " " + str(userDetail.get("lastName")) +": User is not Active")
                    print(inactiveUser)
                    print("----------------------------------")
                    for i in inactiveUser:
                        f.write(i + '\n')
                f.close()

The output that I'm expecting should be like this:

LastName FirstName: User is not Active
LastName2 FirstName2: User is not Active

But instead, I'm having this result.

L
a
s
t
N
a
m
e
 
F
i
r
s
t
N
a
m
e
:
U
s
e
r
i
s
n
o
t
A
c
t
i
v
e

Thanks for your help everyone :)

1 Answers

You are iterating trought inactive users in the wrong place.

for i in inactiveUser:
                    f.write(i + '\n')

this code is itreating trough user string. Instead just do:

f.write(inactiveUser + '\n')

There is no parent loop in the example that will actually iterate trough the inactive users, so that must be fixed also.

Related