Permission error when writing to files using Python 3.8 - python

I am splitting a large wordlist by length of the word
i didn't find a different approach for it so i decided to write a script in python for it.
say test.txt has
word
words
i want it to make new text files based on length of line and write the line to it
4.txt
word
5.txt
words
CODE
import os
import sys
basefile = open(sys.argv[1],'rt')
print("Writing.....")
os.mkdir(str(os.path.splitext(sys.argv[1])[0]))
os.chdir(os.path.splitext(sys.argv[1])[0])
#print(basefile)
for line in basefile:
cpyfile=open(str(len(line.strip()))+'.txt',mode = 'a',encoding = 'utf-8')
cpyfile.write(line)
cpyfile.close()
print("Done")
basefile.close()
It works for small files but for larger files it gives out an error after a while
PermissionError: [Errno 13] Permission denied: '10.txt'
or
PermissionError: [Errno 13] Permission denied: '11.txt'
the error file is completely random too and the previous lines written are perfectly okay.
I have tried it on windows using powershell and using gitbash
Any help is appreciated and thanks

I suspect you are running into the issue that Windows does not allow two programs to open the same file at once. I'm not sure what the second program would be. Maybe a virus scanner? Your program works unaltered on Ubuntu using /usr/share/dict/american-english, so I think this may be a Windows thing.
In any case, I think you can solve this by keeping the files open while the program is running.
import os
import sys
basefile = open(sys.argv[1], 'rt')
print("Writing.....")
os.mkdir(str(os.path.splitext(sys.argv[1])[0]))
os.chdir(os.path.splitext(sys.argv[1])[0])
# print(basefile)
files = {}
try:
for line in basefile:
cpyfilename = str(len(line.strip()))+'.txt'
cpyfile = files.get(cpyfilename)
if cpyfile is None:
cpyfile = open(cpyfilename, mode='a', encoding='utf-8')
files[cpyfilename] = cpyfile
cpyfile.write(line)
finally:
for cpyfile in files.values():
# Not strictly necessary because the program is about to end and
# auto-close the files.
cpyfile.close()
print("Done")
basefile.close()

Related

Make a program delete a file which it just created

Is there a way for me to stop an action in python so that another action in the same python script can start? Or is there a way to split a program into two parts so that they can run after the first part has ended?
import os
t = 'Testfile input...'
f = open('File.txt', 'w')
f.write('{}'.format(t))
os.remove('File.txt')
print('File has been removed')
This is what I always get.
PermissionError: [WinError 32] The process cannot access the file because it is
being used by another process: 'File.txt'
You need to close the file before you can remove it with f.close()
import os
t = 'Testfile input...'
f = open('File.txt', 'w')
f.write('{}'.format(t))
f.close()
os.remove('File.txt')
print('File has been removed')
Otherwise your file system will not allow you to delete it because it still in use.

Script can't save data to file

Based on *.blend file I have to write a script that gets informations about objects and saves them to json. This script can be opened in Blender, or running. The launch should save the json file with the data in the current directory.
So I created this:
import bpy
import json
objects = bpy.context.scene.objects
data = {}
for ob in objects:
item = {}
item['location'] = ob.location
if ob.name == 'Cube':
item['material_name'] = ob.active_material.name
data[ob.name] = item
elif ob.name == 'Camera':
item['camera_type'] = ob.data.type
data[ob.name] = item
elif ob.name == 'Lamp':
item['lamp_type'] = ob.data.type
data[ob.name] = item
with open('scene_objects.json', 'w') as json_file:
json.dump(data, json_file)
However, when I run the script in Blender, I received the following error:
PermissionError: [Errno 13] Permission denied: 'scene_objects.json'
I'm a beginner in using Blender so maybe it's impossible to write to file from Blender? However, if I can do it, I am asking for advice on how?
Your issue isn't with blender, the OS is preventing the creation (or writability) of the file based on file system permissions.
The line -
with open('scene_objects.json', 'w') as json_file:
will create a new file (or open existing) in the current working directory. When running blender that could be one of several options, depending on which OS you are using. It is also possible that starting blender from a GUI can leave you without a valid CWD, or a temporary dir that a user does not have permission to write to.
You can use os.chdir() to change the CWD to one that you know exists and that you can write to. You can also specify a full path instead of just a filename.

Python - Problem with writing file Errno 2

I am a Python newbie and I am having a problem that probably has as a simple answer. I have the following script, which works most of the way, I just get stuck trying the write the output file. The error I get is at the very end: IOError: [Errno 2] No such file or directory: '/D/1_NEW_ANALYSIS/Scripts/Melodic_fsfs/design_Rat01_Run_1.fsf'
Here is the code:
import os
import glob
studydir = 'D:/1_NEW_ANALYSIS'
fsfdir="%s/Scripts/Melodic_fsfs"%(studydir)
templatedir="%s/Scripts/Templates"%(studydir)
subdirs=glob.glob("%s/Subjects/Rat_[0-9][0-9]/Run_[0-2]"%(studydir))
for dir in list(subdirs):
splitdir = dir.split('\\')
# YOU WILL NEED TO EDIT THIS TO GRAB sub001
splitdir_sub = splitdir[1]
subnum=splitdir_sub[-2:]
splitdir_run = splitdir[2]
runnum=splitdir_run[-1:]
print(subnum)
replacements = {'SUBNUM':subnum, 'RUNNUM':runnum}
with open("%s/Melodic_design.fsf"%(templatedir)) as infile:
with open("%s/design_Rat%s_Run_%s.fsf"%(fsfdir, subnum, runnum), 'w') as outfile:
for line in infile:
for src, target in replacements.items():
line = line.replace(src, target)
outfile.write(line)
Anybody have an idea why it doesn't work?
Thanks a lot!
If you are running on windows (I assume you are), studydir should look like:
studydir = 'D:\\1_NEW_ANALYSIS'

Run script on multiple files sequentially?

I have this code which along with the rest of it runs on a single file in a folder.
I want to try and run this code on 11 files in the folder. I have to pass parameters to the whole script via an .sh script which I've written.
I've searched on here and found various solutions which have not worked.
def get_m3u_name():
m3u_name = ""
dirs = os.listdir("/tmp/")
for m3u_file in dirs:
if m3u_file.endswith(".m3u") or m3u_file.endswith(".xml"):
m3u_name = m3u_file
return m3u_name
def remove_line(filename, what):
if os.path.isfile(filename):
file_read = open(filename).readlines()
file_write = open(filename, 'w')
for line in file_read:
if what not in line:
file_write.write(line)
file_write.close()
m3ufile = get_m3u_name()
I have tried a different method of deleting the file just processed then looping the script to run again on the next file as I can do this manually but when I use
os.remove(m3ufile)
I get file not found either method of improving my code would be of great help to me. I'm just a newbie at this but pointing me in the right direction would be of great help.

Delete a file after reading

In my code, user uploads file which is saved on server and read using the server path. I'm trying to delete the file from that path after I'm done reading it. But it gives me following error instead:
An error occurred while reading file. [WinError 32] The process cannot access the file because it is being used by another process
I'm reading file using with, and I've tried f.close() and also f.closed but its the same error every time.
This is my code:
f = open(filePath)
with f:
line = f.readline().strip()
tempLst = line.split(fileSeparator)
if(len(lstHeader) != len(tempLst)):
headerErrorMsg = "invalid headers"
hjsonObj["Line No."] = 1
hjsonObj["Error Detail"] = headerErrorMsg
data['lstErrorData'].append(hjsonObj)
data["status"] = True
f.closed
return data
f.closed
after this code I call the remove function:
os.remove(filePath)
Edit: using with open(filePath) as f: and then trying to remove the file gives the same error.
Instead of:
f.closed
You need to say:
f.close()
closed is just a boolean property on the file object to indicate if the file is actually closed.
close() is method on the file object that actually closes the file.
Side note: attempting a file delete after closing a file handle is not 100% reliable. The file might still be getting scanned by the virus scanner or indexer. Or some other system hook is holding on to the file reference, etc... If the delete fails, wait a second and try again.
Use below code:
import os
os.startfile('your_file.py')
To delete after completion:
os.remove('your_file.py')
This
import os
path = 'path/to/file'
with open(path) as f:
for l in f:
print l,
os.remove(path)
should work, with statement will automatically close the file after the nested block of code
if it fails, File could be in use by some external factor. you can use Redo pattern.
while True:
try:
os.remove(path)
break
except:
time.sleep(1)
There is probably an application that is opening the file; check and close the application before executing your code:
os.remove(file_path)
Delete files that are not used by another application.

Categories

Resources