3

I have the following piece of code to pull the names of all files within a particular folder (including all files in it's subfolders):

import sys,os

root = "C:\Users\myName\Box Sync\Projects\Project_Name"
path = os.path.join(root, "Project_Name")

for path, subdirs, files in os.walk(root):
    for name in files:
        print os.path.join(path, name)

Unfortunately, it throws the following error:

>   File "<ipython-input-7-2fff411deea4>", line 8
>     print os.path.join(path, name)
>            ^ SyntaxError: invalid syntax

I'm trying to execute the script in Jupyter Notebook. I also tried saving it as a .py file and running it through Anaconda prompt but received the same error. Can someone please point out where I'm going wrong? I'm pretty new to Python.

Thanks

Chipmunk_da
  • 467
  • 2
  • 9
  • 27

2 Answers2

3

in python3, print function needs to be like this :

print(os.path.join(path, name))

For more information on the changes within print function from python 2 to 3, check these links :

Mohamed Ali JAMAOUI
  • 14,275
  • 14
  • 73
  • 117
1

This is a Python 2 Vs Python 3 issue.

In Python 2, print is used without parenthesis like:

print 42

In Python 3, print is a function and has to be called with parenthesis like:

print(42)
Matthieu Moy
  • 15,151
  • 5
  • 38
  • 65