I want to extract file names from a folder and be able to use the names as strings:
I can get the file names from the folder but they are going into a list. Ideally I do not want to iterate over the list. Code so far:
from os import listdir
from os.path import isfile, join
f = '/path/to/file/*.sql'
for files in glob.glob(f):
with open(files, 'r',encoding = "utf-8", errors="replace") as f:
path = "/path/to/file/folder"
onlyTxtFiles = [f for f in listdir(path) if isfile(join(path, f)) and f.endswith(".sql")]
not sure how to put the string of the file name into variable 'onlyTxtFiles'.
edit;
I want to be able to do this:
onlyTxtFiles = 'file name'
str1 = onlyTxtFiles + 'test'
edit:
This is a for loop I made, but I am not looping over the files:
f = '/path/to/file/sql_files/*.sql'
for files in glob.glob(f):
with open(files, 'r',encoding = "utf-8", errors="replace") as f:
path = "/path/to/file/sql_files/"
onlySqlFiles = [f for f in listdir(path) if isfile(join(path, f)) and f.endswith(".sql")]
for x in onlySqlFiles:
file_name_insert = x
what am I doing wrong?