hi there,
i'm learning to use python for cgi, and i've hit a few road blocks.
basically, i need to know how to achieve the following things using
python, on linux.
create a new directory, and sub-directory.
so if the user inputs "HELLO", i can create a dir called HELLO, and
then some other dirs in there.
i also need to know how i can write various things into a file.
the following doesnt seem to work for me:
file.write("TEX T HERE")
how can i write plain text into a file?
any help with these questions would be great!
cheers,
paul 2 2287
paul h wrote: hi there, i'm learning to use python for cgi, and i've hit a few road blocks. basically, i need to know how to achieve the following things using python, on linux. create a new directory, and sub-directory. so if the user inputs "HELLO", i can create a dir called HELLO, and then some other dirs in there.
Assuming that:
1. your process's current directory is the one you want to
have as the parent of 'HELLO';
2. your process is running as a user that has permission to
write in its current directory;
3. there is no subdirectory named 'HELLO' in the current
directory;
4. some variable, say X, refers to the string which in this
case happens to be 'HELLO';
then:
import os
os.mkdir(X)
will make that 'HELLO' subdirectory, and then, e.g.:
os.mkdir(X + '/foop')
will create a subdirectory foop of that HELLO subdirectory.
All these points, especially 1-2, cannot be taken for granted
since your process is running as a CGI script; you must ensure
by administrative means that your process is running as the user
you desire (Python or any other language is powerless to help
you there!) and either administrativel y or programmaticall y
ensure the current directory is the one you want (or that you
know the full path to prepend to X) -- the latter may not be
trivial if chroot is in play, as it should be in most well-run
network servers, for security reasons.
i also need to know how i can write various things into a file. the following doesnt seem to work for me: file.write("TEX T HERE") how can i write plain text into a file?
You need to OPEN a file for writing first:
x = file('thefile.t xt', 'w')
and THEN you can call x.write("TEST HERE") to write some string
(in this case, one without a terminating newline). file.write
is an unbound method of built-in type file and, called as you
mean to, would have no possible idea on WHAT file to write on!!!
Alex
paul h wrote: hi there, i'm learning to use python for cgi, and i've hit a few road blocks. basically, i need to know how to achieve the following things using python, on linux. create a new directory, and sub-directory. so if the user inputs "HELLO", i can create a dir called HELLO, and then some other dirs in there.
See os.mkdir() or os.makedirs().
Please be *very* careful letting users access the file system in this
way. Consider the situation where the user enters "/HELLO",
"../../HELLO" etc - they could do all sorts of damage! You should
probably restrict directory creation to a certain area of the file
system. os.path.normpat h() can be useful in working out what the user's
input really means.
i also need to know how i can write various things into a file. the following doesnt seem to work for me: file.write("TE XT HERE") how can i write plain text into a file?
Unless you have created an object reference called file then file is
actually a builtin for *opening* a file, or rather for creating a file
object.
This is probably what you want:
file('thefile.t xt','wt').write ('TEXT HERE')
or better still
f = file('thefile.t xt','wt')
try:
f.write('TEXT HERE')
finally:
if f:
f.close()
which will ensure the file is closed correctly.
Cheers, Matt
--
Matt Goodall, Pollenation Internet Ltd
w: http://www.pollenation.net
e: ma**@pollenatio n.net This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Phil Frost |
last post by:
Greetings all. I'm attempting to embed a python interpreter at a very
low level in an OS I am writing. Currently I'm stuck with build issues.
Firstly, as there is no working C compiler in our OS, I must cross
compile Python. I encountered errors when building Parser/pgen; I got
around this by building it nativly and copying it. Hopefully this won't
get me later...
My current problem (and there are sure to be more ;) ) is when building...
|
by: bray |
last post by:
Learn, share, and have fun at ChiPy's Monthly meeting this Thurs,
April 13 at 7pm.
This is sure to be our best meeting yet. Confirm your attendance:
mtobis aat gmail doot com with "ChiPy April" in your subject line.
Headliners
----------
* httpy -- (Chad Whitacre) a sane and robust HTTP library.
|
by: Peter Otten |
last post by:
QOTW: "It seems if you lurk here long enough you
eventually get all you questions answered without even asking!" - Ted Landis
"We're going to learn from Python. JavaScript is pretty close to Python"
- Brendan Eich
Scott David Daniels shows how to find all occurrences of a string in
another string without using regular expressions.
http://groups.google.com/group/comp.lang.python/msg/f19cdf6de899c755
|
by: Cameron Laird |
last post by:
QOTW: "Write code, not usenet posts." - Fredrik Lundh
"If an embedded return isn't clear, the method probably needs to be
refactored with 'extract method' a few times until it is clear." - John Roth
The comp.lang.python collective has become quite expert
at answering "Which book should I read?" questions.
Note particularly the precision of gene tani, John Salerno,
and Vittorio:
|
by: bray |
last post by:
This will be our best meeting yet! ChiPy's Monthly meeting this Thurs.
August 10, 2006. 7pm. (except for folks who want to help setup at 6:30
and get first dibs on
pizza-compliments of Uncle Roy (Singham))
Topics
------
* Adrian Holovaty's new Django Add-on for quickly publishing websites.
* John Melesky on AppScript
| |
by: bray |
last post by:
Thurs. October 12th, 2006. 7pm.
This will be our best meeting, yet.
David Beasley http://www.dabeaz.com, software developer, writer, and
jazz musician will present on PLY. It's 100% Python and very cool. Do
not miss this one!
Topics
------
|
by: Kurt B. Kaiser |
last post by:
Patch / Bug Summary
___________________
Patches : 416 open (-14) / 3463 closed (+16) / 3879 total ( +2)
Bugs : 930 open ( +8) / 6333 closed (+17) / 7263 total (+25)
RFE : 244 open ( -1) / 244 closed ( +3) / 488 total ( +2)
New / Reopened Patches
______________________
|
by: Krypto |
last post by:
Hi,
I have used Python for a couple of projects last year and I found it
extremely useful. I could write two middle size projects in 2-3 months
(part time). Right now I am a bit rusty and trying to catch up again
with Python.
I am now appearing for Job Interviews these days and I am wondering if
anybody of you appeared for a Python Interview. Can you please share
the questions you were asked. That will be great help to me.
|
by: Mastastealth |
last post by:
I'm trying to create a program to read a certain binary format. I have
the format's spec which goes something like:
First 6 bytes: String
Next 4 bytes: 3 digit number and a blank byte
---
Next byte: Height (Number up to 255)
Next byte: Width (Number up to 255)
Next byte: Number 0 - 5
Every 2 bytes after that: Supposedly a number 0000 - 0899?
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |