WinError 2, python cant find my file when os.remove - python

printing the name of the files goes fine but when I try to use os.remove it says it cannot find the file. I also tried full path but also didnt work.
here is my code
import os
for tempfiles in os.listdir(r"All Posts"):
os.remove(tempfiles)

Related

Been trying to copy folders and files using shutil.tree() function in python but brings FileExistsError

I have tried to use shutil module in python. The shutil.copytree fails to copy folders and brings FileExistsError. I have tried it using different files but it still fails.Please check
import shutil
from pathlib import Path
p = Path(r"C:\Users\rapid\OneDrive\Desktop")
shutil.copytree(p/"folder_1", p/"folder_2")
Please check for the error image
I tried the code myself and works fine the first time.
After the destination folder (in your case folder_2) already created shutil can't create it again and it fails. So you can either delete the destination folder manually or using shutil.rmtree.
Good Luck!
All credit to Timus. He provided the solution on the comment section. Just used
shutil.copytree(p/"folder_1", p/"folder_2", dirs_exist_ok=True)
It works fine. Thanks Timux

Python: FileNotFoundError, from glob output, full path to file is correct

I hate to be the person to post another FileNotFoundError question, but most of them that I see are about not giving the full path to the file, that is not my problem here.
I have a number of log files in folders in ../../Data/. I create a glob of those files using
DataFiles = glob('../../Data/2021*/*.log')
I want to open each of the files in that glob, so I use
for i, file in enumerate(DataFiles):
with open(file, "r") as f:
...
etc. 99% of these open correctly and the rest of the code runs. For some reason, a few will not. I get an error like
FileNotFoundError: [Errno 2] No such file or directory: '../../Data\\20210629_081706\\20210629_081706_data.log'
The file definitely exists, that's why it was found by glob. The full path is used. And,
from pathlib import Path
Path('../../Data\\20210629_081706\\20210629_081706_data.log')
returns
WindowsPath('../../Data/20210629_081706/20210629_081706_data.log')
So does anyone know what might be happening here?
A bit late, but I had the same error when using glob in a network folder with way to many levels.
There was a particular folder where some of the files caused that error, and those files couldn't even be opened by the explorer itself:
In my case this was caused by the path being over 260 characters in length.
You can try something like suggested here to allow handling files with larger paths, or just make sure the path is short enough for the explorer to handle it.

python can not find the fits file when using sys.path.insert

I would like to access the output of another python program that extracts information from a fits file.
I usually do this in the following way:
import sys
sys.path.insert(1, '/software/xray/Python_scripts')
from program2 import results
However, in this case, I receive the following error:
FileNotFoundError: [Errno 2] No such file or directory: 'info.fits'
When I run the program2.py It runs without problem. So, I don't understand why when I call it from program1.py it does not recognize the fits file, therefore it doesn't give the results! Can anybody point me in the right direction?
Thanks.
Seems like your import from program2 import results searching for a file named info.fits which is located most probably in '/software/xray/Python_scripts'.
Basically, sys.path.insert temporarily adds path to PATH, in order to make OS executing/importing script from that place. It doesn't mean that it becomes a file search path as well.
You need to do somethig like:
import os
cwd = os.getcwd()
os.chdir('/software/xray/Python_scripts')
from program2 import results
os.chdir(cwd)
It is certainly a crunch, I would suggest you to create a package out of your program2 module. https://realpython.com/python-modules-packages/

How do I have to solve Pygame couldn't open image error?

background = pygame.image.load('example.jpg')
nubjuk = pygame.image.load('nubjuk.png')
nubjuk = pygame.transform.scale(nubjuk, (200,90))
This is the code part of the problem and the error message is:
Exception has occurred: error
Couldn't open example.jpg
I think I've tried every solution uploaded in other StackOverflow questions.(os.path ~, absolute path, just anything) However, they don't work.
More weird thing is that it properly worked yesterday, but even though I didn't touch anything it doesn't work today.
What's the problem?
Your code expects the files 'example.jpg' and 'nubjuk.png' to be in the current working directory.
Obviously this is not the case. Either you changed the working directory prior to calling your script or your script changes the working directory.
or you moved the png files to a different directory (or deleted them)
If the .png files are in the same directory than your python script you can do something like that
at the beginning of your file.
import os
MYDIR = os.path.realpath(os.path.dirname(__file__))
and then replace your two image.load lines with
background = pygame.image.load(os.path.join(MYDIR, 'example.jpg'))
nubjuk = pygame.image.load(os.path.join(MYDIR, 'nubjuk.png'))

filenotfound error python (running through atom)

I'm working my way through the python crash course pdf. Everything was going well until I hit chapter 10 "files and exceptions".
The task is very simple.
1) create a text file "pi_digits.txt" that contains the first 30 digits of pi.
2) run the following code:
with open('pi_digits.txt') as file_object:
contents = file_object.read()
print(contents)
I keep getting a filenotfounderror [errno 2].
I have tried using the full file path, placing the file in the same ~.atom folder that contains the package 'script'.
I tried to run the file through a terminal and got the same error message.
I also searched stackoverflow for solutions and did find similar problems but the answers did not work.
Any help would be appreciated.
Prepend this:
import os
print(os.getcwd())
os.chdir('/tmp')
and copy the .txt file to /tmp. Also, be sure the copied filename is all lowercase, to match your program.

Categories