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 Overflow
🌐
Reddit
reddit.com › r/learnprogramming › python help. how to write _io.textiowrapper variable type to text file? or convert it to a string?
r/learnprogramming on Reddit: Python Help. How to write _io.TextIoWrapper variable type to text file? Or convert it to a string?
August 20, 2018 -

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

Discussions

TypeError: write() argument must be str, not bytes
Originally reported by: Aurélien Campéas (Bitbucket: auc, GitHub: auc) #!python File "C:\Anaconda2\envs\py3k\lib\site-packages\_pytest\pdb.py", line 109, in post_mortem p.interaction(None, t) File ... More on github.com
🌐 github.com
20
December 8, 2016
python - TypeError: TextIOWrapper.write() take no keyword arguments - Stack Overflow
Been trying to get data to print to an outfile and keep getting this error. When I print the data to the screen (printrec()), everything works, but writerec() does not. I'm not very familiar with p... More on stackoverflow.com
🌐 stackoverflow.com
io - python: TypeError: can't write str to text stream - Stack Overflow
I must be doing something obviously wrong here. But what is it, and how do I fix? Python 2.6.5 (r265:79096, Mar 19 2010, 21:48:26) [MSC v.1500 32 bit (Intel)] on win32 Type "help", "copyright", "c... More on stackoverflow.com
🌐 stackoverflow.com
Gethostbyaddr TypeError: gethostbyaddr() argument 1 must be str, bytes or bytearray, not _io.TextIOWrapper
Hi, new to Python. Just trying to put together a small script to read a file of IP addresses and resolve to name, write back to file. small tool and learning exercise for me. import subprocess import socket tstout = op… More on discuss.python.org
🌐 discuss.python.org
2
0
February 11, 2022
🌐
Medium
medium.com › @pies052022 › typeerror-write-argument-must-be-str-not-bytes-solved-26f91593794e
TypeError: write() argument must be str, not bytes [SOLVED] | by JOKEN VILLANUEVA | Medium
November 19, 2024 - The TypeError: write() argument must be str not bytes error typically occurs if you are trying to write bytes to a file without opening the file in wb mode.
🌐
Arcadiascience
training.arcadiascience.com › arcadia-users-group › 20230328-intro-to-python-3 › lesson
Introduction to Python, Part 3 - Arcadia Science Computational Training
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) Cell In[25], line 4 1 my_text_list = ['Lorem', 'ipsum', 'dolor', 'sit', 'amet'] 3 with open('newfile2.txt', 'w') as file: ----> 4 file.write(my_text_list) TypeError: write() argument must be str, not list
🌐
Solved
sharooq.com › solved-typeerror-write-argument-must-be-str-not-bytes-in-python
Solved - TypeError: write() argument must be str, not bytes in Python
August 24, 2022 - The error "TypeError: write() argument must be str, not bytes" occurs when we try to write bytes to a file which is not opened in the binary mode.
🌐
GitHub
github.com › pdbpp › pdbpp › issues › 63
TypeError: write() argument must be str, not bytes · Issue #63 · pdbpp/pdbpp
December 8, 2016 - Originally reported by: Aurélien Campéas (Bitbucket: auc, GitHub: auc) #!python File "C:\Anaconda2\envs\py3k\lib\site-packages\_pytest\pdb.py", line 109, in post_mortem p.interaction(None, t) File "C:\Anaconda2\envs\py3k\lib\site-package...
Author   pdbpp
🌐
Python
bugs.python.org › issue38029
Issue 38029: Should io.TextIOWrapper raise an error at instantiation if a StringIO is passed as 'buffer'? - Python tracker
September 4, 2019 - This issue tracker has been migrated to GitHub, and is currently read-only. For more information, see the GitHub FAQs in the Python's Developer Guide · This issue has been migrated to GitHub: https://github.com/python/cpython/issues/82210
Find elsewhere
🌐
Python.org
discuss.python.org › python help
Gethostbyaddr TypeError: gethostbyaddr() argument 1 must be str, bytes or bytearray, not _io.TextIOWrapper - Python Help - Discussions on Python.org
February 11, 2022 - Hi, new to Python. Just trying to put together a small script to read a file of IP addresses and resolve to name, write back to file. small tool and learning exercise for me. import subprocess import socket tstout = open('testout.txt','w+') with open("testip.txt", "r") as ins: for l in ins: out = socket.gethostbyaddr(ins) tstout.write (out[0]) tstout.close() subprocess.Popen([r'c:\Windows\notepad.exe',r'c:\scripts\testout.txt']) Get traceback “TypeError: gethostbyaddr() ar...
🌐
Justinbois
justinbois.github.io › bootcamp › 2018 › lessons › l16_file_io.html
l16_file_io
Note also that f.write() only takes strings as arguments. You cannot pass numbers. They must be converted to strings first. ... # This will result in an exception with open('gimme_phi.txt', 'w') as f: f.write('The golden ratio is φ = ') f.write(1.61803398875) --------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-17-9265e8a96772> in <module>() 2 with open('gimme_phi.txt', 'w') as f: 3 f.write('The golden ratio is φ = ') ----> 4 f.write(1.61803398875) TypeError: write() argument must be str, not float
🌐
Python Forum
python-forum.io › thread-19423.html
Preprocess script
Hi all. I'm using Python to process climate data and use it in a weather model. To do this, there was a script in Python2, which I modified to adapt it to Python3. I have a part of the code, which when I execute it gives me the following error: Err...
🌐
GitHub
github.com › tox-dev › tox › issues › 426
TypeError: write() argument must be str, not bytes · Issue #426 · tox-dev/tox
December 14, 2016 - I get the following traceback when there is an error on Linux and Windows with Python 3.5. Not sure which tox version is used. I guess the current one, because I use devpi test and it creates a fre...
Author   tox-dev
🌐
Bobby Hadz
bobbyhadz.com › blog › python-typeerror-expected-str-bytes-or-os-pathlike-object-not-textiowrapper
Expected str, bytes or os.PathLike object, not TextIOWrapper | bobbyhadz
The TypeError: expected str, bytes or os.PathLike object, not TextIOWrapper occurs when we pass a file object instead of a string when opening a file.
🌐
Stack Overflow
stackoverflow.com › questions › 67232782 › typeerror-initial-value-must-be-str-or-none-not-io-textiowrapper
python - TypeError: initial_value must be str or None, not _io.TextIOWrapper - Stack Overflow
I am trying to parse the data that the user enters, I have read through a few other threads and tried fixing this issue but was not able to. TypeError: initial_value must be str or None, not _io.
🌐
Stack Overflow
stackoverflow.com › questions › 68811397 › i-have-this-error-typeerror-textiowrapper-write-takes-exactly-one-argument
python - I have this error: TypeError: TextIOWrapper.write() takes exactly one argument (16 given) when I code - Stack Overflow
The commas: , are making each character separate arguments. But as the error states, .write() supports only 1 argument. What you might want to do is use + to concatenate the strings to 1 single string.
🌐
Open Conversational AI
community.openconversational.ai › archive - mycroft project › archive - former random mycroft category
I am stuck on a python print read problem - maybe someone is kind enough to help - Archive - Former Random Mycroft Category - Open Source Conversational AI Community
July 4, 2019 - Hi there, I’m stuck and probably relatively simple python action. but for the life of me I can not figure it out. it for a skill to read ebooks… it works fine on that part but for the section to continue reading ebooks I am stuck on . basically I just wanted to write a dat file with a name of the book being read and a line count file that keeps tracks of the last line read. i can write the the book path no problem. and read it but can not input it as a variable. and the count i can...
🌐
Glarity
askai.glarity.app › search › How-can-I-resolve-the-"TypeError--write---argument-must-be-str--not-list"-error-in-Python-when-writing-to-a-file
How can I resolve the "TypeError: write() argument must be str, not list" error in Python when writing to a file? - Ask and Answer - Glarity
March 29, 2024 - To fix the "TypeError: write() argument must be str, not list" error when writing to a file in Python, you need to ensure that you are passing a string to the `write()` method, not a list or any other data type.