I made a program in Python 2.7 that renames the files in a directory by replacing the numbers in those file names. I tried to do the same thing with a Golang app but it is not working. The console still gives be new names but it is not changing the names of the photos in the file directory.
Here is the python program
import os
def rename_files():
#(1) get files names from a folder
file_list = os.listdir(r"C:\\Users\\g\\Desktop\\Fun\\udacity\\foundationsofpython\\07finalrenamingapp\\prank")
print(file_list)
saved_path = os.getcwd()
print (saved_path)
os.chdir(r"C:\\Users\\g\\Desktop\\Fun\\udacity\\foundationsofpython\\07finalrenamingapp\\prank")
#2 rename all files names in folder
for file_name in file_list:
print ("Old Name - " +file_name)
print ("New Name - " +file_name.strip("0123456789"))
os.rename(file_name,file_name.strip("0123456789"))
os.chdir(saved_path)
rename_files()
This works just fine. However, this Golang script does not This is the Golang program
package main
import (
"log"
"os"
"fmt"
"regexp"
)
func readCurrentDir() {
dir := "C:\\Users\\g\\Desktop\\prank"
file, err := os.Open(dir)
if err != nil {
log.Fatalf("failed opening directory: %s", err)
}
defer file.Close()
list,_ := file.Readdirnames(0) // 0 to read all files and folders
for _, name := range list {
oldName := name
fmt.Println("Old Name - ", oldName)
re := regexp.MustCompile( "[^A-za-z]" )
newName := re.ReplaceAllString( oldName, " ")
fmt.Println("New Name - ", newName)
os.Rename(dir+oldName, dir+newName)
fmt.Println("File names have been changed")
}
}
func main() {
readCurrentDir()
}
If you want to pull these to projects from the github repo here is a link that will provide you with the code and the photo folder where the images are stored. I would benefit the most by seeing where my code needs to be changed. Any help or advice you could give would be greatly appreciated. Thank you very much.
Python App https://github.com/lashleykeith/GoingwithGolang/tree/master/OldCode2BeChanged
Golang App https://github.com/lashleykeith/GoingwithGolang/tree/master/our_tutorial/5finalrenamingapp