-1

I am trying to learn about Python and in the online class they suggest I run this code:

import os

def rename_files():
    #(1) get file names from a folder
    file_list = os.listdir(r"C:\Users\steph\Desktop\prank")
    print(file_list)

    #(2) for each file, rename filename

In order to print all the items in a folder. However, it doesn't do anything for me. In the shell all that appears is:

Python 2.7.13 (v2.7.13:a06454b1afa1, Dec 17 2016, 20:42:59) [MSC v.1500 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> 
======== RESTART: C:\Users\steph\OneDrive\PythonClass\renamefiles.py ========
>>>

Do you know why? In a previous lesson a similar thing happend: they provided code that would open a web browser - and it worked - but only once! Afterwards the same "RESTART: ..." line would appear and nothing more.

The keywords (at least those that are available to me) are so generic, I cannot find it. I am just starting out with Python and would love to make some progress.

halfer
  • 19,824
  • 17
  • 99
  • 186
SLLegendre
  • 680
  • 6
  • 16
  • It looks like you're using a nonstandard shell. Also, your program will have no output because the `os` module doesn't produce output when imported and the rest is just a function definition that's never called. – TigerhawkT3 Feb 12 '17 at 11:00

1 Answers1

0

You have defined a function rename_files. However, that function is not called anywhere.

Call it by adding an unindented rename_files() at the end. Even better, only call it if your program is being executed, like this:

import os

def rename_files():
    #(1) get file names from a folder
    file_list = os.listdir(r"C:\Users\steph\Desktop\prank")
    print(file_list)

if __name__ == '__main__':
    rename_files()
Community
  • 1
  • 1
phihag
  • 278,196
  • 72
  • 453
  • 469