Creating a DOT file in python - python

I am trying to create a DOT file from python and an error shows up saying :
[Errno 13 permission denied]
enter image description here
This is the code i am using:
Credit_tree_file= open('c:\credit_tree.dot','w')
Can someone help me solve this please.

As a normal user, you are usually not allowed (and, you should not be allowed) to write into the root directory of your disk drive. However, you of course have access to other locations like your own home directory or the temp directory. You can try to create the file in your temp directory, for example like
import tempfile
import os
tmpdir = tempfile.gettempdir()
Credit_tree_file = open(os.path.join(tmpdir, 'credit_tree.dot'),'w')
...

Related

How to set permission for 3 subfolder backwards after creating the directory in python

I would like to create a folder "C://user//Folder1//Folder2//Folder3//Folder4//Folder5//" in python.
"C://user//Folder1//Folder2" exists previously.
When I create the desire folder, I want to allow permission to all Folder3, Folder4 and Folder5. While I don't want to affect the python's working directory which is located in another location "C://user1//python_script".
When I write the script, it only give full permission to Folder5 after creating desire__dir.
import os
desire__dir = "C://user//Folder1//Folder2//Folder3//Folder4//Folder5//"
if not os.path.exists(desire__dir):
os.makedirs(desire__dir)
os.chmod(desire__dir, 0o777)
Try using pathlib when working with paths:
import pathlib
desired_dir = pathlib.Path("C://user//Folder1//Folder2//Folder3//Folder4//Folder5//")
desired_dir.mkdir(parents=True)

Python processing 3 why is current working directory different sometimes?

I am getting this error sometimes when working on mac in Processing for Python. Seemingly for no reason, sometimes the current working directory becomes what you see in the image while other times it is the working directory of the folder the pyde file is in as it should be.
Any ideas on why this is occurring?
It's to avoid problems like these that I always try to use absolute paths. I would suggest you try something like this for file paths:
import os
# This will be the path to your .py file
FILE_PATH = os.path.dirname(os.path.abspath(__file__))
# This will be the path to your text file, if it is in the same directory as the .py
LEVELS_FILE_PATH = os.path.join(FILE_PATH, "levels.txt")
Then, instead of your current open statement you could have:
f = open(LEVELS_FILE_PATH, 'r')

h5py.File(path) doesn't recognize folder path

I am in my project folder call "project". I have two neural network h5 file, one in "project/my_folder/my_model_1.h5", I also copy it to folder "project/my_model_2.h5". So I open my Jupyter Notebook which is working at "project" folder.
import h5py
f = h5py.File("my_model_2.h5") # has NO Issue
but
f = h5py.File("my_folder/my_model_1.h5") # OSError
It says OSError: Unable to open file (unable to open file: name = 'my_folder/my_model_1.h5', errno = 2, error message = 'No such file or directory', flags = 0, o_flags = 0)
Interestingly, I only have this issue when I do the same thing on my Mac, but I don't encounter any issue in Linux machine.
Please let me know if you know how to fix this. Thank you in advance.
So it looks like some hidden invalid character incidentally got copied when I simply copy and paste the file path from Mac folder system. Take a look at the code in the screen.
The Line 92 is the path name I directly copy and paste from Mac folder.
The Line 93 is the path I literally type with every single letter, then there is no error and .h5 file is loaded properly. It's a kinda of similar issue that has been spotted by someone at this link: Invalid character in identifier
I simply copy the error code to Pycharm, and the unwelcome character got busted.
So solution, for Mac user, be careful of of just simply copying the text from folder system, if something obviously weird, try type every letter into the text editor.
Specifying the absolute path using the os worked in windows
file_name = os.path.dirname(__file__) +'\\my_folder\\my_model_1.h5'
f = h5py.File(file_name)
dont forget to import os though

How do I copy all files of one specific type from a folder to another folder in Python

Im trying to copy a numerous amount of .txt files from one folder to another using a Python script. I dont want to copy one single file at a time, and im not even sure exactly how many txt files are present in the folder. I am looking to scan the folder and copy all text files from it to another folder. I have already tried doing this using the shutil and os libraries to no avail. Can someone please help?
import os
import shutil
def start():
dest = "C:/Users/Vibhav/Desktop/Txt"
source = "C:/Users/Vibhav/Desktop/Games"
for file in os.listdir("C:/Users/Vibhav/Desktop/Games"):
if file.endswith(".txt"):
shutil.copy2(dest,source)
This is what I have tried doing, but it doesnt work for me. I keep getting this error
PermissionError: [Errno 13] Permission denied: 'C:/Users/Vibhav/Desktop/Games'
It would really help me if someone could help me out
Main mistake: you're trying to copy the directory, not the file.
Rewriting using glob.glob to get pattern filtering + absolute path sounds the best option:
def start():
dest = "C:/Users/Vibhav/Desktop/Txt"
source = "C:/Users/Vibhav/Desktop/Games"
for file in glob.glob(os.path.join(source,"*.txt")):
shutil.copy2(file,dest)

Python - IOError: [Errno 13] Permission denied:

I'm getting IOError: [Errno 13] Permission denied and I don't know what is wrong wit this code.
I'm trying to read a file given an absolute path (meaning only file.asm),
and a relative path (meaning /.../file.asm), and I want the program to write the file to whatever path is given - if it is absolute, it should write it to the current dir; otherwise, to the path given.
the code:
#call to main function
if __name__ == '__main__':
assem(sys.argv[1])
import sys
def assem(myFile):
from myParser import Parser
import code
from symbolTable import SymbolTable
table=SymbolTable()
# max size of each word
WORD_SIZE = 16
# rom address to save to
rom_addrs = 0
# variable address to save to
var_addrs = 16
# new addition
if (myFile[-4:] == ".asm"):
newFile = myFile[:4]+".hack"
output = open(newFile, 'w') <==== ERROR
the error given:
IOError: [Errno 13] Permission denied: '/Use.hack'
the way I execute the code :
python assembler.py Users/***/Desktop/University/Add.asm
What am I doing wrong here?
Just Close the opened file where you are going to write.
It looks like you're trying to replace the extension with the following code:
if (myFile[-4:] == ".asm"):
newFile = myFile[:4]+".hack"
However, you appear to have the array indexes mixed up. Try the following:
if (myFile[-4:] == ".asm"):
newFile = myFile[:-4]+".hack"
Note the use of -4 instead of just 4 in the second line of code. This explains why your program is trying to create /Use.hack, which is the first four characters of your file name (/Use), with .hack appended to it.
You don't have sufficient permissions to write to the root directory. See the leading slash on the filename?
This happened to me when I was using 'shutil.copyfile' instead of 'shutil.copy'. The permissions were messed up.
I had a same problem. In my case, the user did not have write permission to the destination directory. Following command helped in my case :
chmod 777 University
Maybe You are trying to open folder with open, check it once.
For me nothing from above worked. So I solved my problem with this workaround. Just check that you have added SYSTEM in directory folder. I hope it will help somoene.
import os
# create file
#staticmethod
def create_file(path):
if not os.path.exists(path):
os.system('echo # > {}'.format(path))
# append lines to the file
split_text = text_file.split('\n')
for st in split_text:
os.system('echo {} >> {}'.format(st,path))
Check if you are implementing the code inside a could drive like box, dropbox etc. If you copy the files you are trying to implement to a local folder on your machine you should be able to get rid of the error.
For me, this was a permissions issue.
Use the 'Take Ownership' application on that specific folder.
However, this sometimes seems to work only temporarily and is not a permanent solution.
FYI I had this permission error because the file that it was trying to create was already open/used by another program (was created last time the script was run, I had opened it with excel, and then got a permission error when it was trying to recreate it)
leaving this here in case someone else finds it useful, it is not the real solution to the question asked
I got this error because the directory didn't exist yet.
Solution: create the directory
import os
if not os.path.exists(directory):
os.makedirs(directory)

Categories

Resources