How can i open a folder using python? - python

I've tried reading through the other answered similar questions, yet still can't find what I'm doing wrong.
file=open("Crawl","r")
cont=file.read()
file.close
print(cont)
It's as simple as that. Both the folder and python are on the desktop which explains why there isn't a directory to it. I did try opening .txt and .png files and that did work, but it seems I can't open folders?
PermissionError: [Errno 13] Permission denied: 'Crawl'

You are getting this error because you are trying to open "Crawl" as a file. It's a folder, and you can't treat a Windows folder as if it were a file.

Related

WinError 5 Access is denied: 'C:\\Program Files (x86)

I am trying to delete a temp file which a specific game uses with Python.
I am running CMD as admin and using code:
path = ""
if os.path.exists(path):
os.remove(path)
This is giving me the error access denied likely because it is program files but any way around this?
Based on comments added to the question, it was misleading - you weren't trying to remove a file from Program Files, you were trying to remove a directory from AppData\Local. There shouldn't be any problem with that, except that you're using os.remove to do it. According to the documentation:
If path is a directory, an IsADirectoryError is raised. Use rmdir() to remove directories.
So the fix is simple:
os.rmdir(path)

JupyterLab - python open() function results in FileNotFoundError

I am trying to open an existing file in a subfolder of the current working directory. This is my command:
fyle = open('/SPAdes/default/{}'.format(file), 'r')
The filevariable contains the correct filename, the folder structure is correct (working on macOS), and the file exists.
This command, however, results if this error message:
FileNotFoundError: [Errno 2] No such file or directory: [filename]
Does it have anything to do with the way JupyterLab works? How am I supposed to specify the folder srtucture on Jupyter? I am able to create a new file in the current folder, but I am not able to create one in a subfolder of the current one (results in the same error message).
The folder structure is recognized on the same Jupyter notebook by bash commands, but I am somehow not able to access subfolders using python code.
Any idea as to what is wrong with the way I specified the folder structure?
Thanks a lot in advance.
There shouldn’t be a forward slash in front of SPAdes.
Paths starting with a slash exist high up in file hierarchy. You said this is a sub-directory of your current working directory.

Docx file is locked for editing

I am using the python-docx library to make docx files and then later print them to PDF's. Normally, i use os.remove(file_path)
to get rid of the docx files created during the process. If I do not destroy these files and then run the script again, I get an error because the file is locked for editing. I'm trying to understand what is happening here and what the proper protocol is for dealing with these intermediate files.
Any help would be appreciated.
The script generates a docx file called Example.docx and then converts the docx file to a pdf. If I destroy the docx file, I can run the script again with no problem. If I do not destroy Example.docx, I get this error: PermissionError: [Errno 13] Permission denied: 'Example.docx'
I'm trying to understand why this is occurring.

Python does not have permission to open .txt file

Disclaimer This question might be a duplicate of a question I could not find by searching.
I am trying to open a .txt file I downloaded. I used:
with open("spam.txt", "a+") as ef:
#other code that works
and I am gettingIOError [Errno 13] Permission denied: 'spam.txt'
Is there something I did wrong or do I have to open the file a different way?
Edit: I checked and I do have all the read, write, and execute permissions for the file and I can open it on Notepad. I am using Windows as my OS.
Well, I got the problem fixed. It turned out that Komodo Edit 9.3 needs to be in Admin Mode in order to do anything with files now.
The way to get past it is to run Komodo Edit as Admin or run it from a Python window.
I do not believe it is your code. Are you on a linux environment? If so, check to see if the file has read permissions.
The code works just fine.
with open("white-list.txt","a+") as ef:
print ef
<open file 'white-list.txt', mode 'a+' at 0x10206a6f0>
https://docs.python.org/2/tutorial/inputoutput.html#reading-and-writing-files

"IOError [Errno 13] Permisson denied" when copy a file on Windows

I wrote a program that will copy a file called a.exe to C:/Windows/, then I pack it to exe with PyInstaller, and rename the exe file to a.exe. When I run the exe file, it output IOError [Errno 13] Permisson denied: 'C:/Windows/a.exe', but the file a.exe was copied to the directory C:/Windows. Then I ran it as the Administrator, it happened again...
At first, I copy the file with shututil.copy, then I wrote a function myself(open a.exe, create a.exe under C:/Windows, read a.exe 's content and write to C:/Windows/a.exe, close all), but it doesn't help...Any ideas?
Check if a.exe has read-only attribute. shutil.copy raises "Permission denied" error when it is called to overwrite existing file with read-only attribute set
Apparently you're trying to execute a file that moves itself to a different place ... I guess that cannot work.
Can you copy files that are open in Windows? I have a vague memory that you can't, and the file will be open while you execute it.
Is it really being copied? It doesn't exist there before copying? Did it copy the whole file?

Categories