recursively change file names in directories
<p>Starting with a source directory;</p>

<p>&lt;  C:/Users/Public/Env Defense/Projects/1_Earnings Calls/Quarterly Earnings Calls/1_IOUs by Quarter/2013 Q1/AAA Done/</p>

<p>there are multiple subdirectories within the source directory; for example </p>

<p>&lt; /Users/Public/Env Defense/Projects/1_Earnings Calls/Quarterly Earnings Calls/1_IOUs by Quarter/2013 Q1/AAA Done/AEP</p>

<p>/Users/Public/Env Defense/Projects/1_Earnings Calls/Quarterly Earnings Calls/1_IOUs by Quarter/2013 Q1/AAA DONE/Allete</p>

<p>Within each subdirectory are multiple  files with common words in the names such as;</p>

<p>*2013 Q1 AEP Earnings Call Transcript.txt</p>

<p>*2013 Q1 AEP Earnings Call Transcript.txt</p>

<p>*2013 Q1 ALLETE Earnings Call Transcript.docx</p>

<p>*2013 Q1 AEP Earnings Call Presentation.pdf'</p>

<p>I am writing a script to walk through the subdirectories and remove some of the common words.  For example I want 2013 Q1 AEP Earnings Call Transcript.txt would become 2013 Q1 AEP.txt</p>

<p>The script I wrote is;  FileRename_V1.py</p>

<pre><code>import os
cwd = os.getcwd()
print (' 1 Working Directory is     %s' %cwd)
print (' ')
sourcedir =  '/Users/Public/Env Defense/Projects/1_Earnings Calls/Quarterly          Earnings Calls/1_IOUs by Quarter/2013 Q1/AAA Done'
os.chdir (sourcedir)
cwd = os.getcwd()
print (' 2 Working Directory is     %s' %cwd)
print (' ')
for dirPath, subdirNames, fileList in os.walk (sourcedir):
    for filename in fileList:

    filename = os.path.join (dirPath, filename)

    os.rename(filename,filename.replace("Earnings Call Transcript", ''))
</code></pre>

<p>The result is the following error message; </p>

<p>Traceback (most recent call last):
  File "FileRename_V1.py", line 29, in 
    os.rename(filename,filename.replace("Earnings Call Transcript", ''))</p>

<p>FileNotFoundError: [WinError 2] The system cannot find the file specified: 
'/Users/Public/Env Defense/Projects/1_Earnings Calls/Quarterly Earnings Calls/1_IOUs by Quarter/2013 Q1/AAADONE\2013 Q1 AEP Earnings Call Presentation.pdf'    -> </p>

<p>'/Users/Public/Env Defense/Projects/1_Earnings Calls/Quarterly Earnings Calls/1_IOUs by Quarter/2013 Q1/AAA DONE\2013 Q1 AEP Earnings Call Presentation.pdf'</p>

<p>I have researched  code to recursively rename files and the code I wrote appears similar to examples that worked.  Any suggestions as to what the problem is and how to correct the script will be much appreciated.</p>

<p>Cheers, BobS</p>

<p>The WinError was caused by the file path exceeding 260 characters.  I edited the directory names to reduce the number of characters and the script worked as intended. It is very helpful to be able to quickly edit the names as I am working with 20 directories each of which contains about 40 subdirectories and each subdirectory contains about 5 files.  Fortunate that someone posted the issue with file path length !!</p>

python recursion