简体   繁体   中英

Python 2.7.10 Prompt for user input

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!

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()

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()

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM