python

Python 3.6.0 changing directory and renaming issues  #angularjs #reactjs

  • I’ve succesfully changed directories but I can’t rename the files and I’m not sure why after being able to change directories.
  • join([i for i in file_name if not i.isdigit()])) # This works on my machine os.chdir(saved_path) print(“Our current working directory is “, saved_path) rename_files()
  • ‘s a version that doesn’t require changing the current directory.
  • Changing the directory does not automatically change the value of this (or any other) variable.
  • Now I added this code new_path = os.getcwd() right after changing the directory and I do get the new path but I still can’t change the filenames.

What I’m hoping to accomplish is to scan all the files in the specified directory and remove the numbers that is contained in the name. The problem is I can’t change the directory with the code that is below.

@ng_real_ninja: Python 3.6.0 changing directory and renaming issues #angularjs #reactjs

What I’m hoping to accomplish is to scan all the files in the specified directory and remove the numbers that is contained in the name. The problem is I can’t change the directory with the code that is below.

I’m currently in the Udacity Full Stack Nanodegree Program so if anyone can help me out that is also in the program that would be a plus.

Here is the code:

import os def rename_files(): #(1) Get file names from a folder file_list = os.listdir(“/Users/bill/Documents/web/LocalServer/prank”) #print (file_list) saved_path = os.getcwd() print(“Our current working directory is ” + saved_path) os.chdir(‘/Users/bill/Documents/web/LocalServer/prank’) print(“Our current working directory is “, saved_path) #(2) For each file, rename filename for file_name in file_list: #print(“Old Name – ” + file_name) #print(“New Name – ” + file_name.translate(“0123457689”)) os.rename(file_name, file_name.translate(“0123457689”)) os.chdir(saved_path) print(“Our current working directory is “, saved_path) rename_files()

Here is the output I get:

Update 1:

I’ve finally changed directories but I still can’t rename files. (Ex: 68chicago.jpg to chicago.jpg)

Here is my current code:

import os def rename_files(): #(1) Get file names from a folder file_list = os.listdir(“/Users/bill/Documents/web/LocalServer/prank”) #print (file_list) saved_path = os.getcwd() print(“Our current working directory is ” + saved_path) os.chdir(‘/Users/bill/Documents/web/LocalServer/prank’) new_path = os.getcwd() print(“Our current working directory is “, new_path) #(2) For each file, rename filename for file_name in file_list: #print(“Old Name – ” + file_name) #print(“New Name – ” + file_name.translate(“0123457689”)) os.rename(file_name, file_name.translate(“0123457689”)) print(“Our current working directory is “, new_path) rename_files()

Here is my current output:

Update 2:

I’ve finally solved the problem thanks to @Dan.

Here is his code:

import os def rename_files(): #(1) Get file names from a folder file_list = os.listdir(“/Users/bill/Documents/web/LocalServer/prank”) #print (file_list) saved_path = os.getcwd() print(“Our current working directory is ” + saved_path) os.chdir(‘/Users/bill/Documents/web/LocalServer/prank’) new_path = os.getcwd() print(“Our current working directory is “, new_path) #(2) For each file, rename filename for file_name in file_list: #print(“Old Name – ” + file_name) #print(“New Name – ” + file_name.translate(“0123457689″)) os.rename(file_name, ”.join([i for i in file_name if not i.isdigit()])) # This works on my machine os.chdir(saved_path) print(“Our current working directory is “, saved_path) rename_files()

It looks like you want to change to a dir and then change back to the first directory? If that’s the case you want something like this:

Hey dan thanks for your input. I actually just want to go inside the specified directory. Select all the files in the folder and change their names by removing the numbers that is in front of them (Ex: 68chicago.jpg > chicago.jpg). I’ve succesfully changed directories but I can’t rename the files and I’m not sure why after being able to change directories. – Mr.Bill 6 hours ago

I just ran this code on my machine except I used: os.rename(file_name, “blah”) and it worked as expected so something else must be going on here. I’ve never used translate before, what are you expecting that to do? – Dan Vanatta 6 hours ago

– Mr.Bill 6 hours ago

Yea, I can’t get the translation call to work. I would use something like new_filename = ”.join([i for i in file_name if not i.isdigit()]). I’ve edited my code snippet above it should work now – Dan Vanatta 6 hours ago

Thank you so much!!! It worked. I will have to evaluate the code to make it stick into my head but thank you so much!! – Mr.Bill 5 hours ago

incorrectly. It does not change the file names at all, so rename actually renames X into X:

Try using list comprehension:

Hmm strange. The udacity lesson that I’ve watched used that function to rename the filename but of course the guy was using python 2.7.x and I’m using 3.6.0. What is the proper way of renaming a filename using the latest python version 3.6.x? – Mr.Bill 6 hours ago

Hmm, I’m having some trouble using this method as I have to rename about 55 files that are in the folder. – Mr.Bill 6 hours ago

So what? Create a function that takes a file name and returns its name with all digits removed. – DYZ 6 hours ago

Yeah, I’m currently working on a way to use the provided code but I’m trying to implement a loop that scans all of the files and renames it on how I originally have it instead of just calling the function for each file. Thanks for the code though! – Mr.Bill 6 hours ago

method in the proper manner—your need to pass it a translation table for it to work.

This following works and avoids renaming files that don’t need to be (i.e. doesn’t rename them to their current name). This takes a little extra processing, but is probably faster that performing a useless OS-level operation.

call might raise—which definitely could occur for a number of reasons.

For example:

The code:

Hey @martineau thank you for taking the time for writing down a solution. It’s a unique approach on solving the issue and it does solve my problem. Thank you! – Mr.Bill 5 hours ago

Here’s a version that doesn’t require changing the current directory. Just specify the directory where files are to be renamed. If you specify the full path to the old and new name changing the directory isn’t required.

python