Note that there are some explanatory texts on larger screens.

plurals
  1. POpython function not repeating
    primarykey
    data
    text
    <p>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 '-<em>n</em>' at the end, where <em>n</em> is a number starting at '1' and, if the file with '-1' at the end already exists, '-2' etc.<br> 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<b>-1</b>.txt'.</p> <p>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:</p> <p><em>fileName</em> - input filepath, absolute path. e.g. /Users/foo/sourceFolder/bar.txt<br> <em>index</em> - iterator variable, set to '1' at the start of each file being opened (externally to function) </p> <pre><code>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) </code></pre> <p>Now it seems that the inner call for <em>checkExists()</em> at the end is not running.</p> <p>I hope I have been clear in my explanation.<br> <strong>IAmThePiGuy</strong></p> <p>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.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload