-1

How can I extract the filename from a .pptx file and use that name to create a text file?
I am using this code to read the .pptx file:
f = open(r”dir/path/filename.pptx“,”r”)

maciejwww
  • 1,067
  • 1
  • 13
  • 26
  • I have filename.pptx. I want to create a new text file with filename.txt as name – Sourav Das Sep 23 '20 at 21:34
  • Hi, perhaps use `os.path.splitext(os.path.basename('/my/path'))[0]` – IronMan Sep 23 '20 at 21:36
  • 1
    Does this answer your question? [How to get the filename without the extension from a path in Python?](https://stackoverflow.com/questions/678236/how-to-get-the-filename-without-the-extension-from-a-path-in-python) Did try any kind of research on this problem before posting here? This is a very common issue with many sources online... – Tomerikoo Sep 24 '20 at 11:33

1 Answers1

0

You can extract filename of the '.pptx' file as follows:

import os
filename = [file for file in os.listdir("dir/path/") if file.endswith('.pptx')][0]

Then use it to name file as:

with open(filename.split('.')[0]+'.txt', 'w') as f:
    pass
Hamza
  • 5,373
  • 3
  • 28
  • 43