Read Only 4 First Letters Of .txt File - Python3
I'm trying to read only the 4 letters of a .txt file in my python tool and then set a variable with that 4 letters. In that .txt I export the device arch with adb with subprocess f
Solution 1:
Is filename
a variable representing the file? Or is the file 'platform.txt'
? I think what you want is:
with open('platform.txt', 'r') as myfile:
platform = myfile.read(4)
OR if you want to open both 'platform.txt'
and filename where filename = 'some_file.txt'
, if you're trying to read the platforms from a bunch of files and you want to record them in the file 'planform.txt'
, you could use:
with open(filename, 'r') as myfile, open('platform.txt', 'a') as record:
platform = myfile.read(4)
record.write(platform)
The arguments 'r'
and 'a'
stand for read and append respectively--specifying how the file is going to be used. 'w'
for write is also commonly used, but it will start with the cursor at the beginning of the document, so anything you write will overwrite anything already in the doc.
Solution 2:
As per the python input and output documentation, read()
accepts a maximum number of characters to read, so you can use
platform = myfile.read(4)
Post a Comment for "Read Only 4 First Letters Of .txt File - Python3"