0

I'm working on an online tutorial for Python, & I'm trying to go a little farther for an example problem than it calls for.

The objective is to rename all the files in a folder. My addition is to prompt the user for the folder, rather than hardcoding it.

I've tried the suggestions in Python: user input and commandline arguments, but when I run the script no prompt text is displayed.

As it stands my script looks like this:

import os
import sys
import optparse
def RName_Files():
    #Get the folder to user
    Fol = raw_input("Please enter the folder whose files should have numbers stripped from their name: ") #I've never run past this point

    #Iterate through the files in the folder
    for f in ListDir(f):
        print("Current file is '" + f)

I imagine I'm misunderstanding the answers in the question I linked to, and was hoping someone could clarify the responses for me. Especially since that thread mixes 2.7 and 3.x.

Thanks!

Community
  • 1
  • 1
JMichael
  • 569
  • 11
  • 28

3 Answers3

2

f is undefined when you loop through it. Did you mean ListDir(Fol)? And also ListDir is undefined too.

But above all you are not calling the RName_Files function in your program, try addding RName_Files() at the end of the script.

What could work

import os

ListDir = os.listdir

def RName_Files():
    #Get the folder to user
    Fol = raw_input("Please enter the folder whose files should have numbers stripped from their name: ")

    #Iterate through the files in the folder
    for f in ListDir(Fol):
        print("Current file is '" + f)

if __name__ == "__main__":
    RName_Files()

You should also follow the PEP8 naming conventions for variables and function names. In python variables and functions are snake_case, while class names are CamelCase. And you can also be more clear with your names, rename_files instead of RName_Files, folder or path instead of Fol, file_name instead of f.

Which will look like this:

from os import listdir

def rename_files():
    #Get the folder to user
    path = raw_input("Please enter the folder whose files should have numbers stripped from their name: ")

    #Iterate through the files in the folder
    for file_name in listdir(path):
        print("Current file is " + file_name )
        # do something with file_name 

if __name__ == "__main__":
    rename_files()
f.rodrigues
  • 3,499
  • 6
  • 26
  • 62
  • `f` is supposed to be a variable for the files inside `Fol`. I pretty well carbon copied that from something in the tutorial I'm working on.The only difference is they named the variable `file_name`. The variable name shouldn't matter though, or am I missing something? – JMichael Jul 27 '15 at 20:42
  • You are assigned `f`to be the files, but it doesn't exist anything named `f` when you loop through it, my guess is you should be looping `ListDir(Fol)` instead of `f`. Essentially you are overwriting what `f`is. – f.rodrigues Jul 27 '15 at 20:46
1

You need to call your method

import os
import sys
import optparse
def RName_Files():
    #Get the folder to user
    fol = raw_input("Please enter the folder whose files should have numbers stripped from their name: ") #I've never run past this point

    #Iterate through the files in the folder
    for f in os.listdir(fol):
        print("Current file is '" + f)


RName_Files()
FirebladeDan
  • 1,069
  • 6
  • 14
  • Duh. Sorry, coming from a .Net background, when I hit run while editing something, I'm used to the implicit "run what I'm looking at" – JMichael Jul 27 '15 at 20:43
  • This code won't work, you are defining a local variable `fol` inside `RName_Files`, but using it in the global space. and using `fol` before defining it, since you are calling `RName_Files` after using it. – f.rodrigues Jul 27 '15 at 21:04
  • 1
    He got the idea @f.rodrigues – FirebladeDan Jul 27 '15 at 21:08
0

The "pythonic" way to do this would be like this:

import os
import sys
import optparse
def RName_Files():
    #Get the folder to user
    Fol = raw_input("Please enter the folder whose files should have numbers stripped from their name: ") #I've never run past this point

    #Iterate through the files in the folder
    for f in ListDir(Fol):
        print("Current file is '" + f)

def main():
    RName_Files()

if __name__ == "__main__":
    main()

Basically you defined your function using def but never actually called it. Its good practice to have a main function to call the ones you've created and to call them in this fashion.

amza
  • 780
  • 2
  • 7
  • 32