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?
5
  • If you have tried other questions/ answers please include them in your question, so that your questions is not flagged as duplicate. Commented Jan 16, 2020 at 15:34
  • OK.I will update the question with whatever i have tried Commented Jan 16, 2020 at 15:34
  • 1
    Try os.system('cp -r {} {}'.format(file, destination)) instead of shutil.copy may helps Commented Jan 16, 2020 at 15:35
  • 1
    Also /var is usually sudo only, do you have the according write rights to copy into /var? Commented Jan 16, 2020 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. Commented Jan 16, 2020 at 15:36

1 Answer 1

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)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.