How to put apostrophe and brackets around filenames


Im having issues with loading a file.
I am using anaconda on a windows machine.
I have downloaded some videos which has spaces in their file names.
For example the file name ‘The Emoji Movie’ Official Teaser Trailer (2017) (720p_24fps_H264-192kbit_AAC).mp4.npy
If I want to load the file it would be np.load(“‘The Emoji Movie’ Official Teaser Trailer (2017) (720p_24fps_H264-192kbit_AAC).mp4.npy”)
However I want to put It into a loop for every file i have and i can’t put the apostrophe and brackets in the right place.

Is there a method or function i am supposed to use?

First of all, please don’t post screenshots of the code or error, instead, try to use code formatting like this.

Secondly, you can try using fstrings to define your path and iterate using the variable that holds a value of some part of the filename which is common in all filenames. I have done this in the past but I had consistent filenames.

I tried to use fstrings by creating something like this
z = (F'(" {file_name} ")')
but it would just show up like this
OSError: [Errno 22] Invalid argument: '(" \'The Emoji Movie\' Official Teaser Trailer (2017) (720p_24fps_H264-192kbit_AAC).mp4.npy ")'

Maybe try the following expression:

z = (f'("{file_name}")')

causes same error
OSError: [Errno 22] Invalid argument: '(" \'The Emoji Movie\' Official Teaser Trailer (2017) (720p_24fps_H264-192kbit_AAC).mp4.npy ")'

Wait nvm the issue is from somewhere else I’m dumb

Umm we need to account for the spaces.

z = (f' ('{file_name}') ')

If this doesn’t work, then I am lost as well.

I think the issue is that you do not know what characters are used in the filename.
You can get that information from python by running code like this at the python prompt.

import glob

I have assumed that there is not a space at the end of the filename.

Now you can cut-n-paste the string from the output into your code.


Windows have a lot of restrictions regarding file names. Here you are using double quotes " which are not allowed:

  • We cannot see your code and file names correctly. You have to put them between backticks to show correctly:
    • Wrong: *this* 'will' "get" ^[mangled]this ‘will’ “get” [1]
    • Correct: `*this* 'will' "get" ^[mangled]`*this* 'will' "get" ^[mangled]
  • Use similar markup for longer text (error traceback, code, etc.): Put one line with triple backticks ``` before and another one after the lines of the text. Please do not post screenshots of text.
  • Show your code. We do not know how and which parts of the file name are you substituting in the loop.

  1. mangled ↩︎

1 Like