Python wont find files? - python

I am trying to create code that places watermarks on an image, however Python wont find the background image. I've tried changing directories and changing the format of the name, and I keep getting [Errno 2] and [Errno 22]
im = Image.open("comocasto")

Either you're using the wrong filename, or you're in the wrong directory.

Did you import PIL ?
from PIL import Image
im = Image.open("bride.jpg")
Verify that you are on the correct path
import os
os.getcwd()
Out[24]: 'D:\\'
os.chdir('C:\\')
os.getchw()
Out[27]: 'C:\\'
If this is not the case, them maybe you are running on a server Errno 22 means "invalid argument", do a "ls -l" to see if you have the correct permissions for reading the file.

Turns out it was a school permissions problem involving temporary accounts and Canopy trying to find things in a log folder.

Related

Creating a DOT file in 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')
...

Python permission error on windows 10 when moving/renaming file

I'm just trying to rename a file or move it to another directory. Either option will work for me. I have the most basic code here that I could find on the internet (first python project here by the way).
import shutil
import os
while True:
for file in os.listdir("C:/Users/lines/OneDrive/Desktop/loc1"):
if file.endswith(".txt"):
shutil.move("C:/Users/lines/OneDrive/Desktop/loc1/" + file, "C:/Users/lines/OneDrive/Desktop/loc2/moved.txt")
# os.rename("C:/Users/lines/OneDrive/Desktop/loc1/" + file, "C:/Users/lines/OneDrive/Desktop/loc2/moved.txt")
But no matter what I try, i ALWAYS get this error in my console when running the file:
PermissionError: [WinError 32] The process cannot access the file because it is being used by another process: 'C:/Users/lines/OneDrive/Desktop/loc1/i000130126543220150615123030.txt' -> 'C:/Users/lines/OneDrive/Desktop/loc2/moved.txt'
I truly have no idea how to fix this. Can anyone help?
Edit: both directories are totally empty and only being used for this test purpose. No other text editors have these files open anywhere.

OSError: Initializing from file failed

I am trying to do a simple csv read for my code and it has worked up until I tried tonight for Mac. Currently on 10.15.3, Catalina. This is my code:
df = pd.read_table('/Users/nicholasmori/Desktop/FINAL.csv', delimiter=',')
And the error it gave me was:
OSError: Initializing from file failed.
Ive tried multiple different options to read this csv, including
pd.read.csv(open( ) )
csv.reader( )
pd.read_csv()
with open ( ) as csvfile:
But all these give similar errors. I am sure theres a simple answer, but I haven't been able to find it. Ive tried the
sudo chown username:group filename
command on terminal, and allowed terminal access through my privacy/firewall settings. My file also has Read & Write permissions for "everyone," unless I'm viewing that wrong. Does anyone have a solution for me?
I had the same error, what I did was upload the file to jupyter from my Mac (same root of the python program) and:
import pandas as pd
path_file = "FileName.csv"
data = pd.read_csv(path_file)
Hope works!
When did you upgrade from macOS Catalina (10.15.x) from a previous version (10.14)? OS-level access rights are a bit different.
Also, you can try this, to verify that the file (and path, if provided) are correct:
from pathlib import Path
file_name = 'test.npy'
Path(file_name).is_file() # returns True or False
print(Path.cwd()) # prints current working directory

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