FileX is currently a file pointer, not the context of X.txt. To copy everything from X.txt to Y.txt, you will need to use FileX.read() to write the read content of FileX:
FileY.write(FileX.read())
Perhaps you should also look into using a with statement,
with open("X.txt","r") as FileX, open("Y.txt","w") as FileY:
FileY.write(FileX.read())
# the files will close automatically
And also as suggested by a comment, you should use the shutil module for copying files and/or directories,
import shutil
shutil.copy('X.txt', 'T.txt')
# use shutil.copy2 if you want to make an identical copy preserving all metadata
Answer from Taku on Stack OverflowFileX is currently a file pointer, not the context of X.txt. To copy everything from X.txt to Y.txt, you will need to use FileX.read() to write the read content of FileX:
FileY.write(FileX.read())
Perhaps you should also look into using a with statement,
with open("X.txt","r") as FileX, open("Y.txt","w") as FileY:
FileY.write(FileX.read())
# the files will close automatically
And also as suggested by a comment, you should use the shutil module for copying files and/or directories,
import shutil
shutil.copy('X.txt', 'T.txt')
# use shutil.copy2 if you want to make an identical copy preserving all metadata
str = FileX.readLines()
FileY.write(str)
, pass string instead of the File
Hello!
I am grabbing a some data off of an API. I then am trying to take the variable in which I have stored the gathered data and write it to a text file.
When I define the type my variable is it shows
<class '_io.TextIOWrapper'>
I have tried to figure out how to convert it to a string (as that what the error yells at me for) but I cannot find the function.
import time, json, requests
def btstamp():
bitStampTick = requests.get('https://www.bitstamp.net/api/ticker/')
return bitStampTick.json()['last']
bitstampprice = (btstamp())
print (bitstampprice)
bitstampprice = open("testwrite1.txt", "w+")
print (type(bitstampprice))
bitstampprice.write(bitstampprice)
bitstampprice.close()Here is the error
C:\TradingBot>export.py
6456.95
<class '_io.TextIOWrapper'>
Traceback (most recent call last):
File "C:\TradingBot\export.py", line 15, in <module>
bitstampprice.write(bitstampprice)
TypeError: write() argument must be str, not _io.TextIOWrapper
C:\TradingBot>I have spent a good amount of time doing this and its getting late. Any help would be greatly appreciated.
I go into some info about _io.TextIOWrapper and the info is a bit advance for me.
Why is my data being returned in this type?
How come it is not a integer as it is shown when I "print" the variable?
Thanks
Aluad
TypeError: write() argument must be str, not bytes
python - TypeError: TextIOWrapper.write() take no keyword arguments - Stack Overflow
io - python: TypeError: can't write str to text stream - Stack Overflow
Gethostbyaddr TypeError: gethostbyaddr() argument 1 must be str, bytes or bytearray, not _io.TextIOWrapper
You don't need the line
output.close(), the with-block already closes the file for you.Your indentation is wrong. It should be
with open("output.txt", "w") as output:
x = 1
while x < len(reccomendlist):
output.write("User-ID",x,': ', "Movie-ID",reccomendlist[x][0],', ',"Movie-ID",reccomendlist[x][1],', ',"Movie-ID",reccomendlist[x][2],', ',"Movie-ID",reccomendlist[x][3],', ',"Movie-ID",reccomendlist[x][4], sep = '')
x += 1
- You can iterate over
reccomendlistby doing
for x, element in enumerate(reccomendlist):
element
instead of
x = 1
while x < len(reccomendlist):
element = reccomendlist[x]
x += 1
- You can't add the
sepparameter towrite, this is aprintparameter. Use print with thefileparameter:
for x, element in enumerate(reccomendlist):
print("User-ID",x,': ', "Movie-ID",element[0],', ',"Movie-ID",element[1],', ',"Movie-ID",element[2],', ',"Movie-ID",element[3],', ',"Movie-ID",element[4], sep = '', file=output)
Depending of your Python version, it can be even more readable:
for x, row in enumerate(reccomendlist):
print(f"User-ID{x}", end=': ', file=output)
print(', '.join(f"Movie-ID{element}" for element in row), file=output)
In very short: Instead of using output.write(myClass) use print(myClass, file=output)
The io module is a fairly new python module (introduced in Python 2.6) that makes working with unicode files easier. Its documentation is at: http://docs.python.org/library/io.html
If you just want to be writing bytes (Python 2's "str" type) as opposed to text (Python 2's "unicode" type), then I would recommend you either skip the io module, and just use the builtin "open" function, which gives a file object that deals with bytes:
>>> f1 = open('test.txt','w')
Or, use 'b' in the mode string to open the file in binary mode:
>>> f1 = io.open('test.txt','wb')
Read the docs for the io module for more details: http://docs.python.org/library/io.html
Try:
>>> f1.write(u'bingo') # u specifies unicode
Reference