IOError - PythonAnywhere.com - python

I'm trying to run my web application at www.pythonanywhere.com. The problem is that it loads couple files into the memory and during this process it returns IOError: [Errno 2] No such file or directory:. But I'm sure that the directory is there.
The folder is:
mysite/files/dictionaryA
2015-01-30 15:06:44,101 : File "/home/tox/mysite/Data.py", line 241, in loadDictionaryAB
2015-01-30 15:06:44,102 : with open(path.relpath('files/dictionaryA'),'rb') as f:
2015-01-30 15:06:44,102 :IOError: [Errno 2] No such file or directory: 'files/dictionaryA'
The Data.py is in mysite/files dictionary, so there should be no problem. Linux and Windows on my computer has not problem with that.
I'll appreciate any advices.

The current working directory is where the interpreter was started and not where your .py script is. Either use an absolute path to your files, or make sure you know where you are. os.curdir shows you the current directory. Your home folder can be obtained by expanduser("~") in the os.path module. After you figure out where you are you can easily join the path, or os.chdir() into the folder you require.
from os.path import expanduser
homedir = expanduser("~")
with open(os.path.join(homedir, "mysite/files/dictionaryA"), 'rb') as f:
# Work with dictionaryA
The above should work for your case.

Related

How to make this file execute on another computer - how to have a changeable path?

I am trying to make a keylogger, it works on my computer but when i turn it into an executable it gives me an error "Python failed to execute script keylogger" I think its a path problem because its static and every computer have a diffrent directory, i am a beginner i could use some help.
files = ["log.txt", "info.txt", "clipboard.txt", "screenshot.png"]
dir_path=r"C:/Users/messa/Desktop/Python keylogger/"
##This function to take a screenshot---------------
def takess():
im = ImageGrab.grab()
im.save(dir_path+ "/" + "screenshot.png")
## i have multiple functions tell me if this is not enough .
My solution idea: I tried to check for this path if it does not exist i'll create it, after creating the directory i want to create the files in this directory, but it gives me a permission error " PermissionError: [Errno 13] Permission denied:"
extend = "\\"
dir_path="C:\\Users\\Default\\AppData\\Local"
Path(dir_path).mkdir(parents=True, exist_ok=True)
files = ["log.txt", "info.txt", "clipboard.txt", "screenshot.png"]
for file in files:
f= open(dir_path + extend + file, "w+")
f.close()
You can use pathlib module to get a stored location
import pathlib
dir_path = pathlib.Path().absolute()

Can't delete files - getting FileNotFoundError

I am trying to write a program that will delete files inside a specific folder.
When I run the following code, I get the error:
FileNotFoundError: [Errno 2] No such file or directory: '/Users/joshuamorse/Documents/Programming/python/deletefiles/1.txt
My code:
import os, sys
for filename in os.listdir('/Users/joshuamorse/Documents/programming/python/deletefiles/f2d/'):
print(filename)
x = os.path.realpath(filename) #not required but included so program would give more meaningful error
os.unlink(x)
when line 5 is commented out the program runs fine and lists all the files contained in the folder.
I do not understand why the error is cutting off the last folder (f2d) in the directory path. Additionally if I miss type the last folder in the path to something like f2 it produces the following error: FileNotFoundError: [Errno 2] No such file or directory: '/Users/joshuamorse/Documents/programming/python/deletefiles/f2/.
Why is the last folder included in the path only when it is mispelled? How would I go about fixing this so the correct path is passed?
Solution
Provided by #ekhumoro
os.listdir() does not return the path, just the files names in the specified folder.
Corrected Code
import os
dirpath = '/Users/joshuamorse/Documents/programming/python/deletefiles/f2d/'
for filename in os.listdir(dirpath):
x = os.path.join(dirpath, filename)
os.unlink(x)

PermissionError: [Errno 13] Permission denied # PYTHON

i am trying to copy a file from one folder to another, but i am getting "PermissionError: [Errno 13] Permission denied". I am working within my home directory and i am the administrator of the PC. Went through many other previous posts .. tried all the options that are to my knowledge (newbie to programming) ... need some help.
import os
import shutil
src = "C:\\Users\\chzia\\Scripts\\test" # the file lab.txt is in this folder that needs to be copied to testcp folder.
dst = "C:\\Users\\chzia\\Scripts\\testcp"
for file in os.listdir(src):
src_file = os.path.join(src, file)
dst_file = os.path.join(dst, file)
#shutil.copymode(src, dst) # i have tried these options too same error
#shutil.copyfile(src, dst) # i have tried these options too same error
shutil.copy(src, dst)
My target is to create an .exe that copies a file from the network location to a specific folder on a pc where the .exe is run.
Thanks in advance for all the support and help.
Perhaps try to use shutil.copyfile instead:
shutil.copyfile(src, dst)
Similar old topic on Why would shutil.copy() raise a permission exception when cp doesn't?
I am sure I am late, but I ran into the same problem.
I noticed that in my case the problem is that the subfolder already exists.
If I delete the folder at the start (it is OK in my case).
import os
import shutil
dst = "C:\\Users\\chzia\\Scripts\\testcp" # target folder
def checkFolder(path):
try:
os.stat(path)
shutil.rmtree(path)
except:
os.mkdir(path)
checkFolder(dst)
If you Googled the exception and ended-up here, remember to provide the absolute/full path when using copy & copyfile from shutil. For example,
abs_src_path = os.path.abspath(relative_file_path)
abs_dst_path = os.path.abspath(relative_dst_path)
shutil.copy(abs_src_path , abs_dst_path)
In the question above that is already done, but you might be the one who is mislead by the error message.

Cannot open file in Python "No such file or directory" error

I've been trying to download files from an FTP server. For this I've found this Python-FTP download all files in directory and examined it. Anyways, I extracted the code I needed and it shows as follows:
import os
from ftplib import FTP
ftp = FTP("ftp.example.com", "exampleUsername", "examplePWD")
file_names = ftp.nlst("\public_html")
print file_names
for filename in file_names:
if os.path.splitext(filename)[1] != "":
local_filename = os.path.join(os.getcwd(), "Download", filename)
local_file = open(filename, 'wb')
ftp.retrbinary('RETR ' + filename, local_file.write)
local_file.close()
ftp.close()
But when it tries to open the file, it keeps saying:
ftplib.error_perm: 550 Can't open CHANGELOG.php: No such file or directory
I've tried w+, a+, rw, etc. and I keep getting the same error all the time. Any ideas?
Note: I am using OSX Mavericks and Python 2.7.5.
This question may have been asked several times and believe me I researched and found some of them and none of them worked for me.
open() in Python does not create a file if it doesn't exist
ftplib file select
It looks like you are listing files in a directory and then getting files based on the returned strings. Does nlst() return full paths or just filenames? If its just filenames than retrbinary might be expecting "/Foo/file" but getting "file", and there might not be anything named file in the root dir of the server.

Unable to write file in python

Probably a very noob question..
But
When I try:
f = open(os.path.join(os.path.dirname(__file__), filename),"w")
I get an error
IOError: [Errno 2] No such file or directory: '/home/path/filename'
Isnt it that since i have said "w" .. it will write a new file if its not there already?
The error message can be reproduced like this:
import os
filename = '/home/path/filename'
f = open(os.path.join(os.path.dirname(__file__), filename),"w")
f.close()
# IOError: [Errno 2] No such file or directory: '/home/path/filename'
The problem here is that filename is an absolute path, so
os.path.join ignores the first argument and returns filename:
In [20]: filename = '/home/path/filename'
In [21]: os.path.join(os.path.dirname(__file__), filename)
Out[21]: '/home/path/filename'
Thus, you are specifying not only a file that does not exist, you are specifying a directory that does not exist. open refuses to create the directory.
Are you trying to literally write home/path/filename? In that case, it's complaining that /home/path doesn't exist. Try creating a directory named /home/path or choosing a file name inside a directory that already exists (for example, find out what the path is to your actual home directory.) You can also use relative paths. See
http://en.wikipedia.org/wiki/Path_%28computing%29
for the difference between absolute and relative paths.

Categories