4

Please don't mark the question as a duplicate. I have tried multiple questions and it didn't work for me. hence I am raising a new question.

Problem :

I am trying to copy a list of zip files from my source to the destination folder.I have tried using shutil.copy and shutil.copyfile but I don't see the file getting copied into my destination folder. Can someone help me if I am doing anything wrong or is there any other way of handling zip files. Following is my script and I don't see any error while executing the script. I am running on a Linux machine.

import os,shutil,time
source="/var/lib/jenkins/workspace/JobA/build-output"
destination="/var/lib/jenkins/workspace/JobA/m_output"
mod_files = ["file_1.zip","file_2.zip","file_3.zip","Folder_A","Folder_B"]
os.chdir(source)
if len(mod_files) != 0:
    for file in mod_files:
        if file in os.listdir(source) and file.endswith(".zip"):
            try:
                shutil.copy(file, destination)
                time.sleep(10)
                print("Copied ---- {} ---- to ---- {} ---- \n\n".format(file,
                                                                        destination))
            except Exception as e:
                print("Exception occurred while copying the file to destination \n {}".format(e))
        else:
            raise Exception("{} bundle doesn\'t exist in {} ".format(file, source))
else:
    sys.exit(0)

Questions tried

  1. How do I copy an entire directory of files into an existing directory using Python?
  2. How do I copy a file in Python?
  3. How to copy a file to a specific folder in a Python script?
Auto-learner
  • 1,411
  • 7
  • 26
  • 43
  • If you have tried other questions/ answers please include them in your question, so that your questions is not flagged as duplicate. – LeoE Jan 16 '20 at 15:34
  • OK.I will update the question with whatever i have tried – Auto-learner Jan 16 '20 at 15:34
  • 1
    Try os.system('cp -r {} {}'.format(file, destination)) instead of shutil.copy may helps – Sina Farhadi Jan 16 '20 at 15:35
  • 1
    Also `/var` is usually sudo only, do you have the according write rights to copy into `/var`? – LeoE Jan 16 '20 at 15:36
  • Start with the simplest code that will copy a file. Make sure that works. Write a loop that will iterate all of your files. Make sure that works. Combine. Profit. – Robert Harvey Jan 16 '20 at 15:36

1 Answers1

1

The issue was with the argument you have give for source for the function shutil.copy, so change shutil.copy(file, destination) to shutil.copy(os.path.join(source, file), destination)

Try the code below,

import os,shutil
import time
source = "/var/lib/jenkins/workspace/JobA/build-output"
destination = "/var/lib/jenkins/workspace/JobA/m_output"
mod_files = ["file_1.zip","file_2.zip","file_3.zip","Folder_A","Folder_B"]
if len(mod_files) != 0:
    for file in mod_files:
        if file in os.listdir(source) and file.endswith(".zip"):
            try:
                shutil.copy(os.path.join(source, file), destination)
                time.sleep(10)
                print("Copied ---- {} ---- to ---- {} ---- \n\n".format(file,
                                                                        destination))
            except Exception as e:
                print("Exception occurred while copying the file to destination \n {}".format(e))
        else:
            raise Exception("{} bundle doesn\'t exist in {} ".format(file, source))
else:
    sys.exit(0)
Shibiraj
  • 769
  • 4
  • 9