0

First, I import between one and 25 files named file_1, ..., file_25; if I import seven, it can be file_1 till file_7, but also any of the other files.

Next, I merge them to one. I do this by

files = ["file_1","file_2","file_3","file_4","file_5","file_6","file_7","file_8","file_9","file_10","file_11","file_12","file_13","file_14","file_15","file_16","file_17","file_18","file_19","file_20","file_21","file_22","file_23","file_24","file_25"]
all_files = pd.DataFrame()
for file in files:
    if file in globals():
        print("Merging", file)
        all_files = all_files.append(eval(file))
    else:
        print(file, "not in globals")

(Note for future readers: the list files can easily created by Excel; no need to type it all)

To avoid memory errors, I then want to delete all existing files. I was thinking about something like

a = 1
b = 2
c = 3

l = [a,b,c]

for l in l:
    del l

but it doesn't work (I know that there is no condition for checking the existence in yet, but it also does not work if all files in the list exist. How can this be solved?


Edit: Full example:

file_1 = pd.read_csv("path/file_1")
file_2 = pd.read_csv("path/file_2")
file_3 = pd.read_csv("path/file_3")
file_4 = pd.read_csv("path/file_4")
file_5 = pd.read_csv("path/file_5")
file_6 = pd.read_csv("path/file_6")
file_7 = pd.read_csv("path/file_7")
file_8 = pd.read_csv("path/file_8")
file_9 = pd.read_csv("path/file_9")
file_10 = pd.read_csv("path/file_10")
file_11 = pd.read_csv("path/file_11")
file_12 = pd.read_csv("path/file_12")
file_13 = pd.read_csv("path/file_13")
file_14 = pd.read_csv("path/file_14")
file_15 = pd.read_csv("path/file_15")
file_16 = pd.read_csv("path/file_16")
file_17 = pd.read_csv("path/file_17")
file_18 = pd.read_csv("path/file_18")
file_19 = pd.read_csv("path/file_19")
file_20 = pd.read_csv("path/file_20")
file_21 = pd.read_csv("path/file_21")
file_22 = pd.read_csv("path/file_22")
file_23 = pd.read_csv("path/file_23")
file_24 = pd.read_csv("path/file_24")
file_25 = pd.read_csv("path/file_25")

files = ["file_1","file_2","file_3","file_4","file_5","file_6","file_7","file_8","file_9","file_10","file_11","file_12","file_13","file_14","file_15","file_16","file_17","file_18","file_19","file_20","file_21","file_22","file_23","file_24","file_25"]
all_files = pd.DataFrame()
for file in files:
    if file in globals():
        print("Merging", file)
        all_files = all_files.append(eval(file))
    else:
        print(file, "not in globals")

# How to simplfy the following?
del file_1
del file_2
del file_3
del file_4
del file_5
del file_6
del file_7
del file_8
del file_9
del file_10
del file_11
del file_12
del file_13
del file_14
del file_15
del file_16
del file_17
del file_18
del file_19
del file_20
del file_21
del file_22
del file_23
del file_24
del file_25

I want to be able to import a subset of the files, but still have the following steps (putting them into one varibale and deleting individual files) automated. I succeeded for the first following step (putting them into one varibale), but not for the second following step (deleting individual files). I'd like to somehow loop trough the variables and check if they exist; and if they do, delete them. How to do this?

Julian
  • 591
  • 5
  • 14
  • Unclear. What do you mean by "import"? What kind of files you deal with and what do you expect `all_files.append(eval(file))` to do? What do you mean by deleting files? Either post the entire code or clarify your goals. That said, python is a pass by value language. When you do `l=[a,b,c]` you get `l=[1,2,3]` basically if that makes sense to you. – IcedLance Jun 24 '19 at 10:14
  • Sorry for the lack of clarity. I added a full example and another explanation. – Julian Jun 25 '19 at 08:02
  • I see you've been linked to a different question that will help you improve your code in general, but in case you still want to do what you do: `for variable_name in files: if variable_name in globals(): del globals()[variable_name]` Just add linebreaks. – IcedLance Jun 25 '19 at 10:06

0 Answers0