This question already has answers here:
Line reading chokes on 0x1A
(2 answers)
Closed 8 years ago.
I have a file, which is an assembly source with comments. These comments contain 1Ah character. It is a control character, the "substitute", but it also prints a nice right arrow in DOS, so someone long time ago thought it would be a shame not to use it.
Somehow, it works like end of file character for Python. All I do is:
f = open('file.asm')
for line in f:
print line
f.close()
And everything goes ok just until the first entrance of 1Ah.
The question is, how to read this symbol along with other text?
Open the file using universal line ending support:
f = open('file.asm', 'rU')
This avoids opening the file in native-platform text mode (in the C call) and prevents Windows from interpreting the \x1a code point as a line break.
Try:
f = open('file.asm', 'rb')
It should open file in binary mode.
Related
This question already has answers here:
Why can't I call read() twice on an open file?
(7 answers)
Closed 1 year ago.
I ve been learning about files in python 3.9.6 when this happen:
I open a file using the open command
Write to a file
printed the text with file.read()
the file.read() type is str
print (file.read()) will return nothing .
I am using python 3.9.6 , pycharm community and python IDLE gave the same result, and the problem is , i assumed that if we passed th file.read() which is a string , the print command will be able to actually print it .
>>>file1 = open('t.txt', 'a')
>>>file1.write('hahah')
>>>file1.close()
>>>file1 = open('t.txt', 'r')
>>>file1.read()
'hahah'
>>>print(file1.read())
>>>type(file1.read())
<class 'str'>
As you can read here: https://docs.python.org/3/tutorial/inputoutput.html#methods-of-file-objects
the problem is that you are read the all the file in the first file.read(), which means that you get to the end of the file, so every time (until you will close the file) that you will run file.read() you will get empty string ('').
This question already has an answer here:
Python Read File Content [duplicate]
(1 answer)
Closed 2 years ago.
I try to Python read and then print text from file score.txt (in score.txt is text hrllo world) i write this command:
score = open("data/score.txt", "r")
print(score)
and output is:
<_io.TextIOWrapper name='data/score.txt' mode='r' encoding='cp1250'>
how can i print "hello world" from file score.txt?
You probably want to read the whole filo into the variable in your case.
score = open("data/score.txt", "r").read()
See https://docs.python.org/3/tutorial/inputoutput.html#reading-and-writing-files
I also offer some unsolicited advice: I recommend using a so-called context manager which will automatically close the file after you're done using it (even in case reading the file fails for some reason).
with open("data/score.txt", "r") as score_file:
print(score_file.read())
This is not really very important in your case, but it is an accepted best practice and should be followed whenever possible.
This question already has answers here:
Re.sub not working for me
(2 answers)
Closed 3 years ago.
I want to quickly cancel out some unimportant strings in my file using the python module re.
I've tried it with other modules too, various loops and functions, but all I can get is to find the strings, not replace them with re.sub.
import re
chat = open("chat.txt"
chatText = chat.read()
def deleteMetaData():
for line in chatText:
re.sub("\[\d\d\.\d\d\.\d\d,\s\d\d:\d\d:\d\d\]\sGian\sDogwiler:\s", "", line)
deleteMetaData()
there are no error messages or whatsoever, just nothing. My goal is to cancel [20.07.18, 20:23:09] Gian Dogwiler: in every line of the file.
re.sub() doesn't automatically replace data in a file - you need to write it to the file as well. This answer provides a great example; I've modified it slightly to match your code. Note that you're also missing a close-paren on your chat = open("chat.txt" line.
import re
chat = open("chat.txt")
chatout = open("out.txt", "w")
def deleteMetaData():
for line in chat:
chatout.write(re.sub("\[\d\d\.\d\d\.\d\d,\s\d\d:\d\d:\d\d\]\sGian\sDogwiler:\s", "", line))
deleteMetaData()
chat.close()
chatout.close()
Don't forget to .close() your files when you're done with them.
Try it here!
This question already has answers here:
What does "SyntaxError: Missing parentheses in call to 'print'" mean in Python?
(11 answers)
Closed 5 years ago.
I'm somewhat new to Python, but I have enough under my belt to know what I'm doing. What I'm trying to do is write a few lines for a .txt file (as well as a variable), and then print 5 of those characters.
import os
username = "Chad_Wigglybutt"
file = open("testfile.txt", "w")
file.write("Hello .txt file, ")
file.write("This is a test, ")
file.write("Can this write variables? ")
file.write("Lets see: ")
file.write(username)
file.close()
It then creates the file without issue, but when I add
print file.read(5)
to the code, it gives me a syntax error for file.read, and I have no clue why. I've been on the internet for a few hours now and I can't find anything. Either I'm extremely bad at google searching and I'm an idiot, or something's broken, or both. Any tips/ideas? :/
You're writing Python 3 code. In Python 3, print is a function, not a special statement. You need parentheses for function calls:
print(file.read(5))
This question already has an answer here:
Downloading Mp3 using Python in Windows mangles the song however in Linux it doesn't
(1 answer)
Closed 8 years ago.
I am trying to download a .mp3 file, although, when I download it, it is downloaded but sounds like sqeaks and other strange noises through my speakers (similar to when you have a .mp3 file that has errors in the coding).
Here is my current code:
# sets the song url to mp3link variable.
mp3link = dLink[0]
# opens the .mp3 file - using the same procedure as above.
openmp3 = open('testing.mp3', 'w')
dl = urllib2.urlopen(mp3link)
dl2 = dl.read()
# writes the .mp3 file to the file 'testing.mp3' which is in the variable openmp3.
openmp3.write(dl2)
openmp3.close()
print 'done'
I am aware that I could use this code as a faster method:
dlmp3 = urllib2.urlopen(url)
with open('testing2.mp3', 'wb') as filee:
filee.write(dlmp3.read())
Is there somebody that can tell me what I am doing wrong and how I would be able to fix it?
Thanks.
I found the fix at: Downloading Mp3 using Python in Windows mangles the song however in Linux it doesn't
"Try binary file mode. open(mp3Name, "wb") You're probably getting line ending translations.
The file is binary, yes. It's the mode that wasn't. When a file is opened, it can be set to read as a text file (this is default). When it does this, it will convert line endings to match the platform. On Windows, line ends are \r\n In most other places it's either \r or \n. This change messes up the data stream. "