What's the right way to concatenate files and appending the New Line character in Python? Text or binary mode?

0 votes

Trying to concatenate multiple files in a root to a single file. These are the problems I face:

  • When I've opened each textfile in text file mode the output file shows strange characters (gibberish)
  • When I've opened each textfile in binary mode the output file was corrected.

I'm using Python3 here, so what am I missing?

Nov 13, 2018 in Python by Nymeria
• 3,520 points

edited Dec 14, 2018 by Nymeria 101 views

1 answer to this question.

0 votes

It seems like you're using the wrong encoding for the file in this case. 

If you write a file in one encoding and read it in a different encoding, you will end up getting nonsensical characters made up of the same bytes being interpreted in the wrong way IMHO.

Hope this helps!

answered Nov 13, 2018 by Anirudh
• 2,070 points

edited Dec 14, 2018 by Anirudh

Related Questions In Python

0 votes
1 answer
0 votes
1 answer

What's the canonical way to check for type in Python?

To check if o is an instance ...READ MORE

answered Aug 24, 2018 in Python by Priyaj
• 56,900 points
42 views
0 votes
1 answer

How to handle AssertionError in Python and find out which line or statement it occurred on?

Use the traceback module: import sys import traceback try: ...READ MORE

answered Dec 17, 2018 in Python by charlie_brown
• 7,720 points
603 views
0 votes
1 answer

What is the preferred way to concatenate strings in python??

If the strings you are concatenating are ...READ MORE

answered Dec 20, 2018 in Python by charlie_brown
• 7,720 points
96 views
0 votes
1 answer
0 votes
1 answer

Hadoop Mapreduce word count Program

Firstly you need to understand the concept ...READ MORE

answered Mar 16, 2018 in Data Analytics by nitinrawat895
• 10,710 points
3,317 views
0 votes
1 answer

hadoop.mapred vs hadoop.mapreduce?

org.apache.hadoop.mapred is the Old API  org.apache.hadoop.mapreduce is the ...READ MORE

answered Mar 16, 2018 in Data Analytics by nitinrawat895
• 10,710 points
394 views
0 votes
10 answers

hadoop fs -put command?

put syntax: put <localSrc> <dest> copy syntax: copyFr ...READ MORE

answered Dec 7, 2018 in Big Data Hadoop by Aditya
16,346 views
0 votes
1 answer

Hadoop dfs -ls command?

In your case there is no difference ...READ MORE

answered Mar 16, 2018 in Big Data Hadoop by kurt_cobain
• 9,260 points
1,193 views
0 votes
1 answer

How do I generate some random numbers with a distribution using Python?

scipy.stats.rv_discrete is what you ned IMHO. You can supply ...READ MORE

answered Oct 31, 2018 in Python by Anirudh
• 2,070 points

edited Dec 14, 2018 by Anirudh 141 views