Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 611405
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T17:46:49+00:00 2026-05-13T17:46:49+00:00

I am trying to have a file path like ‘C:\Programfiles\file.txt’ but i would like

  • 0

I am trying to have a file path like ‘C:\Programfiles\file.txt’ but i would like to have file.txt be a variable that i can change whenever i need to. I am trying to compare 2 directories then copy files from one to another if they arent already there. i have this code so far.

import os
import shutil
A= set(os.listdir(r"C:\Users\Morpheous\Desktop\Python Test"))
B= set(os.listdir(r"C:\Users\Morpheous\Desktop\Python Test 2"))
if len(A)< len(B):
    C=B-A
    print("File is: %s" %(C))
    shutil.copy2('C:\\Users\\Morpheous\\Desktop\\Python Test 2\\%r'%(C),'C:\\Users\\Morpheous\\Desktop\\Python Test')

elif len(A) > len(B):
    C=B-A
    print(C)

and i get an error because the variable is inserted into path with {”} around it. How would i go about doing this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T17:46:49+00:00Added an answer on May 13, 2026 at 5:46 pm

    Please use os.path.join to construct paths. Also, you should put the directories in variables for reuse. Furthermore you need to iterate over the difference between the folders (B - A) in order to get each filename that’s in the difference set (C is the set of files that have been added!).

    Here’s the corrected version – tested and working:

    import os
    import shutil
    
    pathA = r"C:\Users\Morpheous\Desktop\Python Test"
    pathB = r"C:\Users\Morpheous\Desktop\Python Test 2"
    
    A = set(os.listdir(pathA))
    B = set(os.listdir(pathB))
    C = B - A
    
    if len(C):
        print("Difference is: %s" % repr(C))
    
        for addedFile in C:
            shutil.copy2(os.path.join(pathB, addedFile),
                         os.path.join(pathA, addedFile))
    else:
        print("No new files")
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to copy file to network path like '\\serverA\destenation\file', but fail with Unauthorized
I have an XML file that I'm trying to serialize into an object. Some
I have a canonical file structure like that (I'm giving sensible names to ease
I would like to have a BAT file open a sql server script. Currently
I am trying to write a program using python-fuse, but I can't get file
I have a regex expression that I'm trying to construct that processes a file
Afternoon, I am trying have an HTML file containing a frameset which contains two
I am trying to have PowerShell unblock a file in Win2K8 R2. Does anyone
I'm trying to have a script automatically transform an xml file into several html
I'm trying to have my application read through a text file and look for

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.