This is what I'm doing. I'm taking a text from a folder, modifying that text, and writing it out to another folder with a modified file name. I'm trying to establish the file name as a variable. Unfortunately this happens:
import os
import glob
path = r'C://Users/Alexander/Desktop/test/*.txt'
for file in glob.glob(path):
name = file.split(r'/')[5]
name2 = name.split(".")[0]
print(name2)
Output: test\indillama_Luisa_testfile
The file name is 'indillama_Luisa_testfile.txt' it is saved in a folder on my desktop called 'test'.
Python is including the 'test\' in the file name. If I try to split name at [6] it says that index is out of range. I'm using regex and I'm assuming that it's reading '/*' as a single unit and not as a slash in the file directory.
How do I get the file name?