I have various JSON files on my drive that I would like to process in a loop, but how can I read them in a loop?
Basically I have a list where all filenames are included and all files are also in one folder.
The objective is to create lists out the json files in a loop.
TestList = ["cats", "dogs"]
for i in TestList:
with open ("{i}.json") as {i}_file:
print({i}_file)
Unfortunately I get syntax errors no matter how I try it.
Thank you so much in advance for your support!
Use:
TestList = ["cats", "dogs"]
for i in TestList:
with open(f"{i}.json") as fp:
print(fp.read())
First, if you use "{i}.json", add the prefix f to define this string as f-strings.
Then your variable {i}_file can't be dynamically evaluated to create the variables cats_file and dogs_file. You have to use a static name.
Related
I have this:
directory = os.path.join("/home","path")
for root,dirs,files in os.walk(directory):
for file in files:
if file.endswith(".csv"):
f=open(file)
f.close()
and 'files' contains about 300 csv files like:
['graph_2020-08-04_2020-08-17.csv',
'graph_2020-04-11_2020-04-24.csv',
'graph_2021-02-05_2021-02-18.csv',
...]
I basically want to add a name to each of these files, so that i have file1, file2, file3 ... for all of them. So if I call file1, it contains graph_2020-08-04_2020-08-17.csv for example. This is what i have:
for i in files:
file[i] = files[i]
But it returns
TypeError: list indices must be integers or slices, not str
What am I doing wrong in my approach?
files is a list with strings in it, not integers. So when you say 'for i in files,' you are telling it that 'i' is a string. Then when you try to do file[i], it gives an error because you 'i' is a string, not an int. So instead of saying 'for i in files', you could say 'for i in range(files.size)' or something like that
You can use the builtin function exec() to execute a string as Python code. An example of how to do this:
file_number = 1 # A counter to keep track of the number of files you have found
directory = os.path.join("/home","path")
for root, dirs, files in os.walk(directory):
for file in files:
if file.endswith(".csv"):
exec("file" + str(file_number) + " = " + file)
file_number += 1
print(file1) # Example usage
I think this is what you wanted to achieve.
Just go with the enumerate(iterable,start) function:
for i,file_name in enumerate(files): # or optionally you can pass a start position which by default is set to 0 and we don't need to modify it in your case
file[i] = file_name
Do remember to initialize a seperate file = [] list as i see you are using file as a loop variable and that way you can't pass it a value and an index,that way the program might throw an error. Do learn about the enumerate() function it saves a lot...Happy Coding..:)
What I understood from your question is that you have a list with n element. and you want to assign each element to n different variables. I can't understand why would you need this but:
#wolfenstein11x is right. with that for loop you'll have each element. However let's say you fix that. With the code you wrote you will assign each element of files to a list named file. (considering it has at least n element)
If what I understood is right and you really want n different variable for each element in files list (still I don't know why would you need it) you might take a look to exec.
Edit after reply:
You can read each file in a loop and do what ever with the content:
directory = os.path.join("/home","path")
for root,dirs,files in os.walk(directory):
for file in files:
with open(file, "r") as conetnt:
print(conetnt.read())
Use for i in range(files.size) instead of for i in files.
basically what the title says, what is the best approach to do this?
I was looking at a few tools like the os.walk and scandir but then I am not sure how I would store them and decide which file to open if they are multiples. I was thinking I would need to store in a dictionary and then decide which numbered item I want.
you can use
list_of_files = os.listdit(some_directory)
which returns a list of names of the files that exist in that directory, you can easily add some of these names to a dictionary based on their index in this list.
Here is a function that implements the specifications you have outlined. It may require some tinkering as your specs evolve, but it's an ok start. See the docs for the os builtin package for more info :)
import os
def my_files_dict(directory, filename):
myfilesdict = []
with os.scandir(directory) as myfiles:
for f in myfiles:
if f.name == filename and f.is_file:
myfilesdict.append(f.name)
return dict(enumerate(myfilesdict))
Does anyone know of a way to name a list in python using a String. I am writing a script that iterates through a directory and parses each file and and generates lists with the contents of the file. I would like to use the filename to name each array. I was wondering if there was a way to do it similar to the exec() method but using lists instead of just a normal variable
If you really want to do it this way, then for instance:
import os
directory = os.getcwd() # current directory or any other you would like to specify
for name in os.listdir(directory):
globals()[name] = []
Each of the lists can be now referenced with the name of the file. Of course, this is a suboptimal approach, normally you should use other data structures, such as dictionaries, to perform your task.
You would be better off using a dictionary. Store the file name as the key value of the dictionary and place the contents inside the corresponding value for the key.
It's like
my_dict = {'file1.txt':'This is the contents of file1','file2.txt':'This is the content of file2'}
I'm trying to create a program that will read in multiple .txt files and rename them in one go. I'm able to read them in, but I'm falling flat when it comes to defining them all.
First I tried including an 'as' statement after the open call in my loop, but the files kept overwriting each other since it's only one name I'm defining. I was thinking I could read them in as 'file1', 'file2', 'file3'... etc
Any idea on how I can get this naming step to work in a for loop?
import os
os.chdir("\\My Directory")
#User Inputs:
num_files = 3
#Here, users' actual file names in their directory would be 'A.txt',
'B.txt', 'C.txt'
filenames = [A, B, C]
j = 1
for i in filenames:
while j in range(1,num_files):
open(i + ".txt", 'r').read().split() as file[j]
j =+ 1
I was hoping that each time it read in the file, it would define each one as file#. Clearly, my syntax is wrong because of the way I'm indexing 'file'. I've tried using another for loop in the for loop, but that gave me a syntax error as well. I'm really new to python and programming logic in general. Any help would be much appreciated.
Thank you!
You should probably use the rename() function in the os module. An example could be:
import os
os.rename("stackoverflow.html", "xyz.html")
stack overflow.html would be the name you want to call the file and xyz.html would be the current name of the file/the destination of the file. Hope this helps!
I have some data in simplejson format in txt files, which I read using:
with open("my_file.txt") as f: any_variable = simplejson.load(f)
It works fine, no problems. However, I now have 100's of such text files (some of which, I dont know the names for!) to read from and I was wondering, if there was a pythonic way to read all these files and assign them to say: any_variable1 to any_variableN. I dont really care in what order they are read in.
Obviously, a simple way would be to loop and store results, yet, I was wondering if there was a pythonic way here.
If the files are inside a directory, you can use:
variables = []
path = "/your/path"
for filename in os.listdir(path):
variables.append(simplejson.load(open(os.path.join(path, filename))))