-1

I have python 3.7.4 and it has f string support. But the server I am porting my data has python < 3.6 and so does not support f string. The following code works fine in local machine:

directory_root = 'dataset_test/'

root_dir = listdir(directory_root)
for animal_folder in root_dir:
        animal_folder_list = listdir(f"{directory_root}/{animal_folder}")

But this code fails in server as it does not support f string. How could I rewrite it using format ?

John
  • 740
  • 5
  • 13
  • 36

3 Answers3

1

You can write it like this:

listdir("{directory_root}/{animal_folder}".format(directory_root=directory_root, animal_folder=animal_folder))

Or

listdir("{}/{}".format(directory_root, animal_folder))
Mushif Ali Nawaz
  • 3,707
  • 3
  • 18
  • 31
0

You can simply use string formatting like following "Your string {0} {1}...{}".format(variable1, variable2, variable)

Rajat Jog
  • 43
  • 1
  • 7
0

The correct way, that works in every environment, not only on your local linux box would be:

animal_folder_list = listdir(os.path.join( directory_root, animal_folder))

When you explicitly specify folder separator (/), it will break on Win-boxes or in other hostile environments. os.path.join() comes to the rescue =)

lenik
  • 23,228
  • 4
  • 34
  • 43