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 4321460
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T08:44:56+00:00 2026-05-21T08:44:56+00:00

I have a python tool to ‘touch’ (utime) a file, then move to another

  • 0

I have a python tool to ‘touch’ (utime) a file, then move to another folder. However, if the file already exists in the destination folder, it is silently overwritten. I would like to check for a file of the same name in the destination folder and, if it exists, rename the one I am moving to its name plus ‘-n‘ at the end, where n is a number starting at ‘1’ and, if the file with ‘-1’ at the end already exists, ‘-2’ etc.
For example, say in the source folder is a file ‘foo.txt’, but there is one ‘foo.txt’ in the destination folder as well. This function should return ‘(absolute path)/foo-1.txt’.

So, I have made a function to check for these circumstances and return the modified string, so I can use rename later and not overwrite. However, currently, if the file exists, it returns nothing. Following is the function code, assuming these vars:

fileName – input filepath, absolute path. e.g. /Users/foo/sourceFolder/bar.txt
index – iterator variable, set to ‘1’ at the start of each file being opened (externally to function)

def checkExists(fileName):
    global index
    print "checkExists(" + fileName + ")"

    if exists(fileName):
        splitPath = split(newFile)
        splitName = splitext(splitPath[1])
        newSplitName = splitName[0] + "-" + str(index)

        index += 1
        newName = splitPath[0] + "/" + newSplitName + splitName[1]

        print "newName = " + newName
    else:
        print "(else) fileName = " + fileName
        print "(else) fileName = " + str(type(fileName))
        print ""
        return fileName

    checkExists(newName)

Now it seems that the inner call for checkExists() at the end is not running.

I hope I have been clear in my explanation.
IAmThePiGuy

P.S. I do not want to hear about potential race problems with utime, I know that the files in the source directory will not be accessed by anything else.

  • 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-21T08:44:56+00:00Added an answer on May 21, 2026 at 8:44 am

    Your problem is that, if the file exists, you don’t return anything. I think you’re intending to use recursion to check the new filename, but you don’t return the result of that call. Here’s a stab:

    def checkExists(fileName, index=0):
        print "checkExists(" + fileName + ")"
    
        if exists(fileName):
            splitPath = split(newFile)
            splitName = splitext(splitPath[1])
            newSplitName = splitName[0] + "-" + str(index)
    
            index += 1
            newName = splitPath[0] + "/" + newSplitName + splitName[1]
    
            print "newName = " + newName
            return checkExists(newName, index)   # recurse
        else:
            print "(else) fileName = " + fileName
            print "(else) fileName = " + str(type(fileName))
            print ""
            return fileName
    

    I also took the liberty of moving the recursive call closer to the generation of newName and removing the global variable and replacing that with recursion as well.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is it possible to have Python save the .pyc files to a separate folder
I have a tool which I have written in python and generally should be
I have Python 2.6 and I want to install easy _ install module. The
If I have Python code class A(): pass class B(): pass class C(A, B):
If I wanted to have Python distributed across multiple processors on multiple computers, what
Does Python have a unit testing framework compatible with the standard xUnit style of
Does python have the ability to create dynamic keywords? For example: qset.filter(min_price__usd__range=(min_price, max_price)) I
Does Python have extension methods like C#? Is it possible to call a method
I have a Python module installed on my system and I'd like to be
I have a Python application in a strange state. I don't want to do

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.