How to add prefix to files' names, replace a character, and move to new directory?
<p>For example, the files will look like <code>FG-4.jpg</code>  <code>FG-5.jpg</code>, etc. and need to be copied to a new directory and named <code>test_FG_4.jpg</code> <code>test_FG_5.jpg</code>, etc. </p>

<p>Here is the updated code:</p>

<pre><code>import shutil
import glob
import os
InFolder = r"C:\test_in"
OutFolder = r"C:\test_out"

for f in glob.glob('*'):
    shutil.move(InFolder/*, OutFolder, copy_function=copy2)
    os.listdir(OutFolder)
    new_filename = f.replace("-","_")
    new_filename = "test_" + new_filename
    os.rename(f,new_filename)
</code></pre>

<p>I'm getting the error </p>

<pre><code>File "c:\copyRename2.py", line 8, in ?
  shutil.move(InFolder/*, OutFolder, copy_function=copy2)

invalid syntax: copyRename2.py, line 8, pos 26 in file c:\copyRename2.py, line 8
shutil.move(InFolder/*, OutFolder, copy_function=copy2)
</code></pre>

<p>First attempt:   </p>

<pre><code>import shutil
import glob
import os
InFolder = r"C:\test_in"
OutFolder = r"C:\test_out"

for f in glob.glob('*'):
    shutil.copyfile(f, OutFolder)
    new_filename = f.replace("-","_")
    new_filename = "test_" + new_filename
    os.rename(f,new_filename)
</code></pre>

<p>I am not sure about what you want. So this program checks for any file with extension .jpg and then copies them into a new folder("NewDir") by adding "Test_" to the file name. If the folder doesn't exist, the program creates the folder. Maybe you can make the changes you need based on this program.</p>

<pre><code>import shutil
import os
newdir="NewDir"
for m in (os.listdir()):
    if m[-4:]==(".txt"):

        if os.path.isdir(newdir):
            shutil.copy(m,newdir+"/"+"Test_"+m)
        else:
            os.mkdir(newdir)
            shutil.copy(m,newdir+"/"+"Test_"+m)
</code></pre>

python replace rename