Sorting a file then storing that into a new file using Python
so I have to sort a file in alphabetical order which isn't my problem. I
can do that, but my problem is writing that sorted list into a new file.
Here is what I have.
import os
os.chdir("/Users/User/Documents") #(I put my name where it says "User")
user_file = open("info.txt", "r")
file = user_file.read()
file_string = file.split("\n")
file_string.sort()
print(file_string) #This is here just so I can see that it sorted correctly
end_file = open("write.txt", "w")
for item in file_string:
end = end_file.write(str(item))
print(end)
Ok, so my info.txt is what needs to be alphabetized and it is just a list
of colors. So my last 4 lines is where I'm struggling. I'm trying to take
each item in the file_string and write those into a new file, but
print(end) gives me how many characters they are. What am I doing wrong?
Thanks
No comments:
Post a Comment