I am trying to open a file using the full path, but I don't know the complete name of the file, just a unique string within it.
i = identifier
doc = open(r'C:\my\path\*{0}*.txt'.format(i), 'r')
Now obviously this doesn't work because I'm trying to use the wildcard along with the raw string. I have had a lot of trouble in the past trying to use file paths without preceding them with 'r', so I'm not sure of the best way to handle the incomplete file name. Should I just forget raw string notation and use '\\\\' for the file path?
opendoesn't work with wildcards. Also, the best way to work with the backslashes in your DOS pathnames is to useos.path.jointo build the path.path=glob.glob(r'path/*identifier*.txt')thendoc=open(path, 'r')?glob.globreturns a possibly empty list, so you can't directly open its return value. Butpaths=glob.glob(...); if paths: doc = open(paths[0], 'r')is basically right. It appears thatglobis forgiving about which file dividers you use.