I have a Django App which creates PDF files on the server. On the development server it works well but on IIS it doesnt create the file. I have given all the persmission yet theres no luck. I wrote a simple script to write a text file and that too doesnt work on IIS. Any help to resolve this is appriciated.
Thank you.
def fileWriteTest(request):
f = open("media/testwrite.txt", "a")
f.write("Now the file has more content!")
f.close()
print("File Written!")
return HttpResponse("Done !")
FileNotFoundError at /accounts/testfile/
[Errno 2] No such file or directory: 'media/testwrite.txt'
I found a work around.
'''
from django.conf import settings
import os
def fileWriteTest(request):
my_file = os.path.join(settings.MEDIA_ROOT, str("testwrite4.txt"))
f = open(my_file, "a")
f.write("Now the file has more content!")
f.close()
print("File Written!")
return HttpResponse(str(my_file))
'''
Related
I tried using open but it gives an error that the folder doesn't exist, which makes no sense since this is a command to create a folder, not read one. I saw Automatically creating directories with file output, but there is an error saying this is a Errno 30: Read only system: "/folder". Does anyone know how to avoid Error 30?
My code so far:
import os
filename = "/folder/y.txt"
os.makedirs(os.path.dirname(filename), exist_ok=True)
with open(filename, "w") as f:
f.write("FOOBAR")
I figured i just shouldn't put the slash behind the "folder"filename = "folder/y.txt" os.makedirs(os.path.dirname(filename), exist_ok=True) with open(filename, "w") as f: f.write("FOOBAR")
I'm trying to access a folder and read the text files in it. Eventually I'm going to build them into a basic dictionary to dump into JSON but something's going on with the files. I'm getting an error message:
FileNotFoundError: [Errno 2] No such file or directory: ___
The thing is, this code IS working on my test files ... so maybe it's somehow a file issue?
import json
import os
for file in os.listdir("filepath"):
with open ('%s.json' % file, 'w') as fp:
file = f.read()
f = open(file, 'r')
print (f)
These are the text files not getting 'found/read':
https://drive.google.com/open?id=0B4zJC6biI6jERDFHSzZvUUJaRE0
Has anyone else run into this problem / found a solution that might work?
The code below 1. Identifies files that are created in a directory and 2. Uploads them to my webserver.
My problem is that the program is only successful when I copy and paste a file into the directory "path_to_watch". The program fails when I use a third-party program (NBA Live 06) that creates files in the "path_to_watch" directory.
The error I receive is: "PermissionError: [Errno 13] Permission denied: 'filename.txt'"
import os, time
from ftplib import FTP
def idFiles():
path_to_watch = r"c:\Users\User\gamestats"
before = dict ([(f, None) for f in os.listdir (path_to_watch)])
while True:
time.sleep (1)
after = dict ([(f, None) for f in os.listdir (path_to_watch)])
added = [f for f in after if not f in before]
if added:
## edit filename to prepare for upload
upload = str(", ".join (added))
ftp = FTP('www.website.com')
ftp.login(user='username', passwd='password')
ftp.cwd('archives')
## error is called on this following line
ftp.storbinary('STOR ' + upload, open(upload, 'rb'))
#resets timer
before = after
idFiles()
Many thanks in advance for any help.
If the third party program has opened the files in an exclusive mode (which is the default) then you cannot open them yourself until it has let go of them.
Considering that it's third party code you can't change the mode in which the file is open, but you'll have to wait for the program to close the files before trying to manipulate them.
See also this question
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.
I tried to read a file in a view like this:
def foo(request):
f = open('foo.txt', 'r')
data = f.read()
return HttpResponse(data)
I tried to place the foo.txt in almost every folder in the project but it still returns
[Errno 2] No such file or directory:
'foo.txt'
So does anybody knows how to open a file in app engine patch? Where should i place the files i wish to open? many thanks.
I'm using app-engine-patch 1.1beta1
In App Engine, patch or otherwise, you should be able to open (read-only) any file that gets uploaded with your app's sources. Is 'foo.txt' in the same directory as the py file? Does it get uploaded (what does your app.yaml say?)?
Put './' in front of your file path:
f = open('./foo.txt')
If you don't, it will still work in App Engine Launcher 1.3.4, which could be confusing, but once you upload it, you'll get an error.
Also it seems that you shouldn't mention the file (or its dir) you want to access in app.yaml. I'm including css, js and html in my app this way.
You should try f = open('./foo.txt', 'r')