4 Replies - 433 Views - Last Post: 10 July 2019 - 10:42 AM Rate Topic: -----

#1 albert003   User is offline

  • D.I.C Addict

Reputation: 37
  • View blog
  • Posts: 745
  • Joined: 15-December 14

Copy and replace files

Posted 28 June 2019 - 11:20 AM

Trying to make a program for work to fix the problems we have with old out dated computers that are abused by the employees when they're not shut down properly.

My goal is to have a thumb drive with the good files and when a computer crashes I can copy the missing file from my thumb drive to the companies computer. I realise it would be easier to just reinstall windows but they have proprietary software in the computers and they won't let me do that.

I have 2 questions my program runs, but I wanted to add some features to make it more user friendly. I will be the only one using it because my supervisor views writing code as magic.

1. To copy a file successfully I had to add the lines of code to the function
      if source != target:
                print("File copied")
            else:
                shutil.move(source,target)

What did I do wrong that I need to have those lines of code there?.

2. I wanted to add the feature similar to when you run cmd in windows. If the file exists in the current directory it will give you the option to copy over it or not copy it. I put some conditions and no matter what I select (yes or no) it will still copy the file.

This is the complete program

import os
import shutil

def ListFiles():
    os.chdir("d:/")#"c:/"
    os.listdir(os.curdir)
    x = os.listdir(os.curdir)
    for i in x:
        print(i)
         
def CopyFile():
    pick = input("Which directory do you want to enter?:")
    #C:\windows
    os.chdir("{}".format(pick))
    os.listdir(os.curdir)
    x = os.listdir(os.curdir)
    for i in x:
        print(i)
    source = input("Enter source file with full path: ")
    target = input("Enter target file with full path: ")
    shutil.copy(source,target)
    if source == source:
        print("Do you want to copy over existing file? (yes/no)")
        choice = input()
        if choice in ("yes","ys","y"):
            if source != target:
                print("File copied")
            else:
                shutil.move(source,target)
        elif choice in ("no","n"):
            print("File not copied")
            main()
    elif source != source:
        if source != target:
            print("File copied")
        else:
            shutil.move(source,target)

        
def Maintenance():#This function will run chkdsk and disk defrag
    pass

def main():
    listy = ["Copy file",
             "List of files",
             "Device maintenance",
             "Exit"]
    choice = None
    while (choice!=4):
        try:
            for i, v in enumerate(listy):
                print(i+1,v)
            print("Choose an option:")
            choice = (int(input()))
            if choice == 1:
                CopyFile()
            elif choice == 2:
                ListFiles()
            elif choice == 3:
                Maintenance()
            elif choice == 4:
                break
            else:
                print("Choose one of the options:") 
        except:
            print("Invalid entry. Choose one of the options:")
                    

main()


This post has been edited by albert003: 28 June 2019 - 11:21 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Copy and replace files

#2 DK3250   User is offline

  • Pythonian
  • member icon

Reputation: 553
  • View blog
  • Posts: 1,730
  • Joined: 27-December 13

Re: Copy and replace files

Posted 28 June 2019 - 11:44 AM

I'm not sure I can help with your question.

Line 22 and 33, however, seem quite peculiar:
if source == source:  # line 22
if source != source:  # line 33

I guess 22 is always True and 33 is always False...!

In line 32 you call main().
Are you aware that this is a recursive call?
Why not just return - this will bring you back to the main while loop.

This post has been edited by DK3250: 28 June 2019 - 11:44 AM

Was This Post Helpful? 1
  • +
  • -

#3 albert003   User is offline

  • D.I.C Addict

Reputation: 37
  • View blog
  • Posts: 745
  • Joined: 15-December 14

Re: Copy and replace files

Posted 01 July 2019 - 10:47 AM

I was trying to be too fancy and add a feature that if the user tries to copy an existing file to a folder it would give the option whether to copy it or not.

This is the way I originally had it without the extra feature.

def CopyFile():
    pick = input("Which directory do you want to enter?:\n")
    os.chdir("{}".format(pick))
    os.listdir(os.curdir)
    x = os.listdir(os.curdir)
    for i in x:
        print(i)
    source = input("Enter source file with full path:\n")
    target = input("Enter target file with full path:\n")
    shutil.copy(source,target)
    if source != target:
        print("File copied")
    else:
        shutil.move(source,target)

Was This Post Helpful? 0
  • +
  • -

#4 DK3250   User is offline

  • Pythonian
  • member icon

Reputation: 553
  • View blog
  • Posts: 1,730
  • Joined: 27-December 13

Re: Copy and replace files

Posted 08 July 2019 - 03:23 PM

You say that you have to add those lines:
    if source != target:
        print("File copied")
    else:
        shutil.move(source,target)

What happens if you don't add them? An error? Or...?

Both 'source' and 'target' are entered by the user (line 8,9 in post #3). They should never be the same - or you copy the file to itself.
If they are not the same, the else-part never executes.
So, all those 4 lines do is printing 'File copied' to the console.
Was This Post Helpful? 0
  • +
  • -

#5 albert003   User is offline

  • D.I.C Addict

Reputation: 37
  • View blog
  • Posts: 745
  • Joined: 15-December 14

Re: Copy and replace files

Posted 10 July 2019 - 10:42 AM

You were right, I went line by line and it works without those lines in it.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1