Opening multiple json files inside a for loop - python

I have some code that saves a json file and prints it to screen. I am trying to find the best way to iterate through a directory of files, printing one file after another, but I am receiving an '[Errno 13] Permission Denied' error.
At present I am doing the following:
json_path = 'MYPATH'
json_files = [f for f in os.listdir(json_path) if f.endswith('.json')]
for jf in json_files:
with open (os.path.join(json_path)) as my_jf:
json_text = json.load(my_jf)
print(json_text)
I have made sure that the folder in the path is not opened elsewhere, and I have access to it. If there is a simpler way to achieve this I would appreciate the input.

You are not really open the files, you are opening the path where the files are located.
You could try to change:
with open (os.path.join(json_files)) as my_jf:

I have stumbled across an answer of sorts. If I create a list of the text files in the directory the json.load request seems to work:
my files = ['file1.txt', 'file2.txt']
for file in myfiles:
with open(file) as json_file:
jsonconvo = json.load(json_file)
print(jsonconvo)
I'm unsure if I've necessarily overcome the the actual issue, but this seems like a reasonable workaround.

Looks like you just forgot to actually include the file name in your open() statement:
with open(os.path.join(json_path, jf)) as my_jf:

Related

Filetypes that Shutil.move and os.rename cannot transfer

I am new to python. I tried to search for answers but I cannot find a exact match to my question. I am trying to move all non-Excel files to another folder. However, there is an error when trying to move a .pbix file. I wonder if there are only limited number of filetypes supported by shutil.move() and os.rename() in moving files. And, are there any workarounds? Thank you.
UPDATE: The error is PermissionError. Actually, when I checked now the target folder, the file is transferred but the original file is retained.
Here is my sample code:
files = os.listdir(os.getcwd())
for f in files:
try:
data = pd.read_excel(f) # importing the file
except:
shutil.move("{}".format(f), r".\\Non_Excel_Files\{}".format(f))
It is now working. Thanks to the suggestion of S3DEV.
files = os.listdir(os.getcwd())
for f in files:
if os.path.splitext(f)[1] != ".xlsx":
shutil.move("{}".format(f), r".\\Non_Excel_Files\{}".format(f))

Write multiple text files to the directory in Python

I was working on saving text to different files. so, now I already created several files and each text file has some texts/paragraph in it. Now, I just want to save these files to a directory. I already created a self-defined directory, but now it is empty. I want to save these text files into my directory.
The partial code is below:
for doc in root:
docID = doc.find('DOCID').text.strip()
text = doc.find('TEXT').text,strip()
f = open("%s" %docID, 'w')
f.write(str(text))
Now, I created all the files with text in it. and I also have a blank folder/directory now. I just don't know how to put these files into the directory.
I would be appreciate it.
========================================================================
[Solved] Thank you guys for your all helping! I figured it out. I just edit my summary here. I got a few problems.
1. my docID was saved as tuple. I need to convert to string without any extra symbol. here is the reference i used: https://stackoverflow.com/a/17426417/9387211
2. I just created a new path and write the text to it. i used this method: https://stackoverflow.com/a/8024254/9387211
Now, I can share my updated code and there is no more problem here. Thanks everyone again!
for doc in root:
docID = doc.find('DOCID').text.strip()
did = ''.join(map(str,docID))
text = doc.find('TEXT').text,strip()
txt = ''.join(map(str,docID))
filename = os.path.join(dst_folder_path, did)
f = open(filename, 'w')
f.write(str(text))
Suppose you have all the text files in home directory (~/) and you want to move them to /path/to/dir folder.
from shutil import copyfile
import os
docid_list = ['docid-1', 'docid-2']
for did in docid_list:
copyfile(did, /path/to/folder)
os.remove(did)
It will copy the docid files in /path/to/folder path and remove the files from the home directory (assuming you run this operation from home dir)
You can frame the file path for open like
doc_file = open(<file path>, 'w')

Creating empty text files with specific names in a directory

I have two directories:
dir = path/to/annotations
and
dir_img = path/to/images
The format of image names in dir_img is image_name.jpg.
I need to create empty text files in dir as: image_name.txt, wherein I can later store annotations corresponding to the images. I am using Python.
I don't know how to proceed. Any help is highly appreciated. Thanks.
[Edit]: I tried the answer given here. It ran without any error but didn't create any files either.
This should create empty files for you and then you can proceed further.
import os
for f in os.listdir(source_dir):
if f.endswith('.jpg'):
file_path = os.path.join(target_dir, f.replace('.jpg', '.txt'))
with open(file_path, "w+"):
pass
You can use the module os to list the existing files, and then just open the file in mode w+ which will create the file even if you're not writing anything into it. Don't forget to close your file!
import os
for f in os.listdir(source_dir):
if f.endswith('.jpg'):
open(os.path.join(target_dir, f.replace('.jpg', '.txt')), 'w+').close()

Opening a file in a folder in Python

I want to open a file to write to.
with open('test.txt','a') as textfile:
...
It works like this.
Now I want this file to be opened/created from a directory called args.runkeyword.
with open(os.path.join(args.runkeyword, 'test.txt'),'a') as textfile:
t says it can't find test/test.txt (supposing runkeyword is test).
I also tried by appending with os.getcwd() but it still can't find or create the file.
Any ideas?
os.getcwd() is irrelevant on your work actually. Use os.listdir() to see every folder in a directory. If anything named by test before it may be problem.
A recursive function like this may usefull for you;
import os
def tara(directory):
start = os.getcwd()
files = []
os.chdir(directory)
for oge in os.listdir(os.curdir):
if not os.path.isdir(oge):
files.append(oge)
else:
files.extend(tara(oge))
os.chdir(start)
return files
file = open('test.txt', 'a+')
You should have 'a+' not 'a', the + allows you to append.

Skip first row in python

I am using
for file in fileList:
f.write(open(file).read())
I am combining files if a folder to one csv. However I dont need X amount of headers in the one file.
Is there a way to use this and have it write everything but the first row (header) coming from the files in the files?
Use python csv module
Or something like that:
for file_name in file_list:
file_obj = open(file_name)
file_obj.read()
f.write(file_obj.read())
This solution doesn't load whole file into memory, so when you use file_obj.readlines(), whole file content is load into memory
Note, that it isn't good practice to name variables with builtin names
for file in fileList:
mylines = open(file).readlines()
f.write("".join(mylines[1:]))
This should point you in the right direction. Please don't do your homework on stackoverflow.
If it's a cvs file, look into python csv lib.

Categories

Resources