Dear Tutor,

I have made some progress!

But not yet got the results.

Attached is revised code.


Specifically, the problem in below:


for root, dirs, files in os.walk(".", topdown=False):
    for name in files:
        print(os.path.join(root, name))
        os.rename(path + name, path + name.replace("---", "changed"))
        #os.rename(path + "\\"+ name, path + "\\"+ name.replace("---", 
"changed")
        files[name] = os.sep.join([dirpath, name])

        print (files)

    for name in dirs:
        print(os.path.join(root, name))


.\---DAT1\---DAT3\---000010.txt


---------------------------------------------------------------------------
FileNotFoundError                         Traceback (most recent call last)
<ipython-input-6-316f0c967a9b> in <module>()
      2     for name in files:
      3         print(os.path.join(root, name))
----> 4         os.rename(path + name, path + name.replace("---", "changed"))
      5         #os.rename(path + "\\"+ name, path + "\\"+ name.replace("---", 
"changed")
      6         files[name] = os.sep.join([dirpath, name])

FileNotFoundError: [WinError 2] The system cannot find the file specified: 
'E://---000010.txt' -> 'E://changed000010.txt'


Thanks once again for your time spent on this.

best,
banda.
+

________________________________
From: banda gunda
Sent: Friday, August 11, 2017 8:10 AM
To: tutor@python.org
Subject: conditional renaming folder and files in the tree


Dear Tutor,

I want to change the name of the folders and the files in the tree.
All those folders and files starting with name string '---'.
Examples:
If a  folder name is : \---DAT1
I want to change this to: \changedDAT1
If a file name is: \---00001.txt
I want to change this to: \changed00001.txt

I have attached the code and output to this email.
Specifically, I like to provide correct syntax (dst / destination) for line 6 
code block below!
I have not understood the syntax.

Thanks in advance, for your help .
best,
banda
+


for root, dirs, files in os.walk(".", topdown=False):
    for name in files:
        print(os.path.join(root, name))
        os.rename(name.replace("---", "changed"))
        list_of_files[name] = os.sep.join([dirpath, name])
        print (list_of_files)

    for name in dirs:
        print(os.path.join(root, name))


.\---DAT1\---00001.txt


---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-6-b679212713d0> in <module>()
      2     for name in files:
      3         print(os.path.join(root, name))
----> 4         os.rename(name.replace("---", "changed"))
      5         list_of_files[name] = os.sep.join([dirpath, name])
      6         print (list_of_files)

TypeError: Required argument 'dst' (pos 2) not found

_end_of_email

                                                                      In [1]:

 import os
 import sys
 import glob

                                                                      In [2]:

 print ("Current directory is %s: " %os.getcwd())

 Current directory is C:\Users\mysku\AnacondaProjects:

                                                                      In [3]:

 path = "E://"

 # Check current working directory.
 retval = os.getcwd()
 print ("Current working directory %s" % retval)

 # Now change the directory
 os.chdir( path )

 # Check current working directory.
 retval = os.getcwd()

 print ("Directory changed successfully %s" % retval)

 Current working directory C:\Users\mysku\AnacondaProjects
 Directory changed successfully E:\

                                                                      In [4]:

 # listing directories
 print ("The dir is: %s"%os.listdir(os.getcwd()))

 The dir is: ['---DAT1', '---DAT2', '---00007.txt', '---00008.txt', 
'---00009.txt']

                                                                      In [5]:

 for root, dirs, files in os.walk(".", topdown=False):
     for name in dirs:
         print ("names of folders are: ")
         print(os.path.join(root, name))
     for name in files:
         print("names of files in folders are: ")
         print(os.path.join(root, name))


 names of files in folders are:
 .\---DAT1\---DAT3\---000010.txt
 names of folders are:
 .\---DAT1\---DAT3
 names of files in folders are:
 .\---DAT1\---00001.txt
 names of files in folders are:
 .\---DAT1\---00002.txt
 names of files in folders are:
 .\---DAT1\---00003.txt
 names of files in folders are:
 .\---DAT2\---00005.txt
 names of files in folders are:
 .\---DAT2\---00006.txt
 names of files in folders are:
 .\---DAT2\---00007.txt
 names of folders are:
 .\---DAT1
 names of folders are:
 .\---DAT2
 names of files in folders are:
 .\---00007.txt
 names of files in folders are:
 .\---00008.txt
 names of files in folders are:
 .\---00009.txt

   this works for root folders

   for fileName in os.listdir("."): os.rename(fileName,
   fileName.replace("---", "changed"))

                                                                      In [6]:

 for root, dirs, files in os.walk(".", topdown=False):
     for name in files:
         print(os.path.join(root, name))
         os.rename(path + name, path + name.replace("---", "changed"))
         #os.rename(path + "\\"+ name, path + "\\"+ name.replace("---", 
"changed")
         files[name] = os.sep.join([dirpath, name])

         print (files)

     for name in dirs:
         print(os.path.join(root, name))

 .\---DAT1\---DAT3\---000010.txt

 ---------------------------------------------------------------------------
 FileNotFoundError                         Traceback (most recent call last)
 <ipython-input-6-316f0c967a9b> in <module>()
       2     for name in files:
       3         print(os.path.join(root, name))
 ----> 4         os.rename(path + name, path + name.replace("---", "changed"))
       5         #os.rename(path + "\\"+ name, path + "\\"+ name.replace("---", 
"changed")
       6         files[name] = os.sep.join([dirpath, name])

 FileNotFoundError: [WinError 2] The system cannot find the file specified: 
'E://---000010.txt' -> 'E://changed000010.txt'
_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor

Reply via email to