Python Forum
How to "copy" a file in python?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to "copy" a file in python?
#1
So I am trying to not use shutil or os libraries. So what i tried is:
    fileOriginal = open(filename, "rb")
    os.chdir(destination) #In this case destination is a full path.
    fileDuplicate = open(filename, "wb")
    fileDuplicate.write(fileOriginal)
    fileOriginal.close()
    fileDuplicate.close()
This is what i have so far. But this throws this error: 

TypeError: a bytes-like object is required, not '_io.BufferedReader'

How am I supposed to do it using this method. 
I need this to work for any file type.
Reply
#2
fileDuplicate.write(fileOriginal.read())

fileDuplicate is an object. You can't just write it to a file. To get the content of a file object you have to call its method read()



Instead of changing the directory you can use os.path.join() method

>>> import os.path

>>> destination = "Programming/Pycode"

>>> file = "2en-adv.py"

>>> os.path.join(destination, file)
'Programming/Pycode/2en-adv.py'
fileDuplicate = open(filename, "wb")
Also, consider using a 'with' statement to open the files.

with open(filename, 'rb') as fileOriginal, open(os.path.join(destination, filename), 'wb') as fileDuplicate:
    fileDuplicate.write(fileOriginal.read())
This way Python closes the opened files once the with block is executed
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#3
Um, what does os.path.join do? I don't understand that part.
Reply
#4
(Mar-29-2017, 08:38 AM)tannishpage Wrote: Um, what does os.path.join do? I don't understand that part.
Making a absolute path so Python can read it from file system.
>>> import os
>>> help(os.path.join)
Help on function join in module ntpath:

join(a, *p)
   Join two or more pathname components, inserting "\" as needed.
   If any component is an absolute path, all previous path components
   will be discarded.

>>> path = 'c:/music/app/'
>>> file_name = 'song.mp3'
>>> os.path.join(path, file_name)
'c:/music/app/song.mp3'

>>> # Linux
>>> path = 'music/app/'
>>> file_name = 'song.mp3'
>>> os.path.join(path, file_name)
'music/app/song.mp3'
Reply
#5
Ok. Thanks. Your solution worked, and i understand it.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Why is the copy method name in python list copy and not `__copy__`? YouHoGeon 2 249 Apr-04-2024, 01:18 AM
Last Post: YouHoGeon
  Copy Paste excel files based on the first letters of the file name Viento 2 422 Feb-07-2024, 12:24 PM
Last Post: Viento
  is it possible to copy image from email and place into excel file? cubangt 3 1,263 Nov-30-2022, 05:11 PM
Last Post: snippsat
  Please help me [copy and paste file from src to dst] midomarc 2 1,003 Nov-24-2022, 10:13 PM
Last Post: midomarc
  Copy column from one existing excel file to another file mkujawsk 0 5,605 Apr-14-2021, 06:33 PM
Last Post: mkujawsk
  Copy documents to Teams using python SallySmith 0 2,379 Mar-23-2021, 04:27 AM
Last Post: SallySmith
  Failing to copy file from a network to a local drive tester_V 4 7,091 Jan-20-2021, 07:40 AM
Last Post: tester_V
  Copy mp3 file multiple times and rename Mar10 4 3,732 Sep-23-2020, 01:09 AM
Last Post: Mar10
  Python Cut/Copy paste file from folder to another folder rdDrp 4 5,037 Aug-19-2020, 12:40 PM
Last Post: rdDrp
  Cannot copy file to another location rcmanu95 1 1,727 Jul-19-2020, 05:16 AM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020