How to write content of one file to another file in Python?

0 votes
I have a text file containing 10 lines of data such as maddy:1190, johny:1123 and so on. Now I want another text file with only names such as maddy, johny and so on. How to do it?
Aug 26, 2019 in Python by Arvind
• 2,980 points

1 answer to this question.

0 votes
The following code snippet might solve your problem. Have a look at it.

my_file = open('abc.txt', 'w')
for line in lines:
    var1, var2 = line.split(",");
    my_file.write("%s\n" % var1)

answered Aug 26, 2019 by Neel
• 3,010 points

Related Questions In Python

0 votes
1 answer

How do I append one string to another in Python?

If you only have one reference to ...READ MORE

answered Oct 22, 2018 in Python by SDeb
• 13,250 points
0 votes
1 answer

How to check latest change time of each file using glob in python?

You can get the changing time from ...READ MORE

answered Jan 2, 2019 in Python by Omkar
• 69,030 points
0 votes
1 answer

How to write data to a file in Python?

Refer to the below code. data=’whatever your data ...READ MORE

answered May 12, 2019 in Python by Shaam
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 6, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 1,367 views
0 votes
0 answers
+4 votes
6 answers

Lowercase in Python

You can simply the built-in function in ...READ MORE

answered Apr 11, 2018 in Python by hemant
• 5,800 points
0 votes
1 answer

How to get permutations of list or a set in Python?

Permutation is an arrangement of objects in ...READ MORE

answered Jul 11, 2019 in Python by Neel
• 3,010 points
+1 vote
1 answer

How to print a pyramid of asterisks in Python?

This is one of the most frequently ...READ MORE

answered Aug 1, 2019 in Python by Neel
• 3,010 points