Django write to csv in views not working - python

In my views I have:
import csv
outputfile = open("test.csv", 'w')
I am getting a [Errno 13] Permission Denied
So I tried just opening the file:
outputfile = open("test.csv")
I am getting a [Errno 2] No such file or directory: 'test.csv' when I try to open it.
When I run the same line of code using python manage.py shell, the file opens without an error. test.csv is in the same folder as my views.
Why am I getting these errors and how do I go about fixing them?

Related

I want to change the cwd and ling with mention file name using open to just read the file but shows the below error

I am using open to link file after change the CWD, but I'm getting following error :
FileNotFoundError: [Errno 2] No such file or directory: 'C:\\Users\\Ahmed.sriea\\Desktop\\tables\\Ahmed.text'
Code:
import os
print(os.getcwd())
print(os.path.abspath(__file__))
print(os.path.dirname(os.path.abspath(__file__))) ch=os.chdir(r'C:\Users\Ahmed.Sriea\Desktop\tables')
print(os.getcwd())
myfile= open(r'C:\Users\Ahmed.sriea\Desktop\tables\Ahmed.text')

Python script is returning FileNotFoundError on Mac, but not Linux

I am having an error with my script. I am on Linux (Ubuntu), and my script runs fine... but when I send it to someone who uses macOS, they get this error:
FileNotFoundError: [Errno 2] No such file or directory: 'mytext.txt'`
with open('mytext.txt', 'r') as f:
file = f.readlines()
Of course, both me (and Google) thought it was because of relative path. I changed it to absolute path, but same error.
import os
with open(os.getcwd() + '/mytext.txt', 'r') as f:
file = f.readlines()
Well, that didn't work. Kept searching, and I tried this solution:
import os
path_name = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'mytext.txt')
with open(path_name, 'r') as f:
file = f.readlines()
...but that didn't work either.
Any ideas as to why this may be? Every iteration of the script worked for me, the Linux user, but not for the macOS user. Is there something I'm missing about Linux and macOS Python script cross-compatibility, or something in my actual code I'm just doing wrong? And yes, I am sure there is a mytext.txt in the exact same directory as the actual script for both of us. Thanks for assisting me!
Edit: When trying to get help with this question in a different location, someone asked for the full error. In case it helps, here it is:
Traceback (most recent call last):
File "/Volumes/drive2/test/mytext.py", line 15, in <module>
with open(path_name, 'r') as f:
IOError: [Errno 2] No such file or directory: '/Volumes/drive2/test/mytext.txt'
And to reiterate, it works fine for me (and other people who are not on macOS). Error only applies for the person trying to run this on macOS.
So I tested this solution here that you proposed on a macOS with jupyter notebook:
import os
with open(os.getcwd() + '/mytext.txt', 'r') as f:
file = f.readlines()
and it worked just fine for me, so this is not a macOS problem. The person that is trying to run this has to make sure that both the notebook/ python script as well as the .txt file are in the same folder though.

Python - [Errno 13] Permission denied. Not able to open the file

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
with open(r"E:\Paper\codedata.tsv", 'r+') as temp_f:
Error:
Traceback (most recent call last):
File "E:\Paper\code.py", line 18, in <module>
with open(r"E:\Paper\codedata.tsv", 'r') as temp_f:
What I have tried so far:
Changed file permissions. I am using Windows 10
Windows is upto Date
Working in Anaconda spyder. Tried opening spyder in admin and running the code
Changed directory of data to different path
I am using Python 3.

Error when reading pickle file - permission error

I'm trying to read a pickle file.
df= pd.read_pickle(r'C:\\Users\\Downloads\\pickle_file')
PermissionError: [Errno 13] Permission denied: 'C:\\Users\\Downloads\\pickle_file'
I have no other instances of python running. It's my first time importing the pickle file. What causes this issue?

IDLE on windows error PermissionError: [Errno 13] Permission denied:

I am using IDLE on windows , when i run this below code i get error .
mut = 'F:\Perl\python\Examples'
file_name = open (mut,'r')
Traceback (most recent call last):
File "", line 1, in
file_name = open (mut,'r')
PermissionErroenter code herer: [Errno 13] Permission denied: 'F:\Perl\python\Examples'
'F:\Perl\python\Examples' is the path where my 'mut' file is located.
Please assists i am confused ?
'/' can be used on Windows quite happily, and is simpler.
Looks like Examples is a directory/folder - it should be a filename. That is why you are getting the error.
Note that open() returns a file handle, not a file name. What are you expecting this code to do?
Are you perhaps looking for os.walk() or os.listdir()?

Categories