473,395 Members | 1,535 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

raw_input just continues anyway?

Hey Everyone,

This is probably going to sound like a bit of a stupid question - but
why does (in the following code) the script just continue to run past
the raw_input, when the user hasn't entered anything?

if __name__ == "__main__":
bucket_name = raw_input('Name of the bucket you wish the files to be
placed into? ')
update_s3()

Basically, it just asks the question and continues anyway?

Many Thanks,
Oliver

Apr 3 '07 #1
6 2244
On Apr 3, 8:27 am, "oli...@obeattie.com" <oli...@obeattie.comwrote:
Hey Everyone,

This is probably going to sound like a bit of a stupid question - but
why does (in the following code) the script just continue to run past
the raw_input, when the user hasn't entered anything?

if __name__ == "__main__":
bucket_name = raw_input('Name of the bucket you wish the files to be
placed into? ')
update_s3()

Basically, it just asks the question and continues anyway?

Many Thanks,
Oliver
This code works on my machine: Python 2.4, Windows XP Pro SP2

Apr 3 '07 #2
ol****@obeattie.com wrote:
if __name__ == "__main__":
bucket_name = raw_input('Name of the bucket you wish the files to be placed into? ')
update_s3()

Basically, it just asks the question and continues anyway?
It reads stdin until a line break. Then it continues. Exactly what
behaviour do you get?

Regards,
Björn

--
BOFH excuse #328:

Fiber optics caused gas main leak

Apr 3 '07 #3
Hi There,

Here's the full code, if it helps:

"""
Takes a list of filenames via standard input and uploads them to
Amazon S3.

Requires S3.py:
http://developer.amazonwebservices.c...&categoryID=47

Usage:
cd /directory/with/media/files/
find | grep -v ".svn" | python /path/to/update_s3.py

Before you use this, change the aws_access_key_id, aws_secret_key and
bucket_name variables at the top of the file.

You can run this multiple times on the same files -- it'll just
override the
files that were in your S3 account previously.
"""

import mimetypes
import os
import settings
import os.path
import sys
os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'
from custom_filters import filename_slugify
import S3
import settings

aws_access_key_id = settings.AWS_ACCESS_KEY_ID
aws_secret_key = settings.AWS_SECRET_KEY

def update_s3(bucket_name=bucket_name):
conn = S3.AWSAuthConnection(aws_access_key_id, aws_secret_key)
for line in sys.stdin:
filename = os.path.normpath(line[:-1])
if ((filename == '.' or not os.path.isfile(filename)) or
(filename.find('ds_store') >= 0)):
continue # Skip this, because it's not a file.
print "Uploading %s" % filename_slugify(filename)
filedata = open(filename, 'rb').read()
content_type = mimetypes.guess_type(filename)[0]
if not content_type:
content_type = 'text/plain'
conn.put(bucket_name, filename_slugify(filename),
S3.S3Object(filedata),
{'x-amz-acl': 'private', 'Content-Type': content_type})

if __name__ == "__main__":
bucket_name = raw_input('Name of the bucket you wish the files to be
placed into? ')
update_s3()
Basically I pipe some files into the script - so would this cause a
linebreak?

Apr 3 '07 #4
In <11**********************@w1g2000hsg.googlegroups. com>,
ol****@obeattie.com wrote:
if __name__ == "__main__":
bucket_name = raw_input('Name of the bucket you wish the files to be
placed into? ')
update_s3()
Basically I pipe some files into the script - so would this cause a
linebreak?
Yes of course. `raw_input()` is reading from `stdin` so the very first
line you pipe in will be assigned to `bucket_name`.

Ciao,
Marc 'BlackJack' Rintsch
Apr 3 '07 #5
How could I resolve this?

Many thanks,
O

Apr 3 '07 #6
ol****@obeattie.com wrote:
How could I resolve this?
Try to keep each answer so it makes sense on its own, please. I presume
you are responding to this:
In <11**********************@w1g2000hsg.googlegroups. com>,
ol****@obeattie.com wrote:
if __name__ == "__main__":
bucket_name = raw_input('Name of the bucket you wish the files to be
placed into? ')
update_s3()
Basically I pipe some files into the script - so would this cause a
linebreak?

Yes of course. `raw_input()` is reading from `stdin` so the very first
line you pipe in will be assigned to `bucket_name`.
from Marc 'BlackJack' Rintsch?

Essentially you seem to want to use standard input for two separate
purposes: one to provide the name of a bucket (which it appears you
would like to specify interactively) and the other to provide a list of
filenames that your script has to process.

I would suggest in that case providing the bucket name as a command-line
argument. That way standard input can be limited to communicating
filenames, and your pipeline doesn't need to contain two separate types
of information. Your code would then become (without error checking):

if __name__ == '__main__':
import sys
bucket_name = sys.argv[1]
update_s3()

I assume from this code, by the way, that update_s3() makes use of the
bucket name as a global variable. While this will work it isn't best
practice. You might want to consider passing bucket_name as an argument
to update_s3() (and you won't need to change the code of update_s3() if
you call the argument bucket_name too).

Hope this serves to help rather than confuse.

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://del.icio.us/steve.holden
Recent Ramblings http://holdenweb.blogspot.com

Apr 3 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Hugh | last post by:
I am using python 2.3 through the PythonWin program on windows. I would like to create a console-based interactive session. The program raw_input is almost exactly what I'd like, except that...
2
by: J. W. McCall | last post by:
I'm working on a MUD server and I have a thread that gets keyboard input so that you can enter commands from the command line while it's in its main server loop. Everything works fine except that...
0
by: dale | last post by:
Python newbie disclaimer on I am running an app with Tkinter screen in one thread and command-line input in another thread using raw_input(). First question - is this legal, should it run...
21
by: planetthoughtful | last post by:
Hi All, As always, my posts come with a 'Warning: Newbie lies ahead!' disclaimer... I'm wondering if it's possible, using raw_input(), to provide a 'default' value with the prompt? I would...
13
by: Paraic Gallagher | last post by:
Hi, This is my first post to the list, I hope somebody can help me with this problem. Apologies if it has been posted before but I have been internet searching to no avail. What I am trying...
7
by: Mike Kent | last post by:
It's often useful for debugging to print something to stderr, and to route the error output to a file using '2>filename' on the command line. However, when I try that with a python script, all...
8
by: Dox33 | last post by:
I ran into a very strange behaviour of raw_input(). I hope somebody can tell me how to fix this. (Or is this a problem in the python source?) I will explain the problem by using 3 examples....
1
by: BurnTard | last post by:
Okay fellow scripters, I have a problem. It's small but annoying. See, I wrote this program for encrypting messages. The program itself runs like a charm, except for one major issue. I'm sure I've...
5
by: jamitwidme | last post by:
Is there any way to type into a Tkinter frame window? I want to use raw_input() within a Tkinter frame.
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
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,...
0
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...
0
Oralloy
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,...
0
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.