473,320 Members | 2,104 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,320 software developers and data experts.

Output strings to directory locally

Is it possible to output strings created from a print statement into a directory locally?
If so, would you use sysstdout module?
Sep 8 '14 #1
7 1385
bvdet
2,851 Expert Mod 2GB
You can assign sys.stdout to an open file object.
Expand|Select|Wrap|Line Numbers
  1. f = open("test.txt", 'w')
  2. sys.stdout = f
Subsequent print statements will direct the output to the open file.
Sep 8 '14 #2
Thank you! However, my files do not have a .etc extension. They are unique and I need them to actually be propagated straight into a directory. Is this possible?
Sep 8 '14 #3
bvdet
2,851 Expert Mod 2GB
It's a good idea to save the old value of sys.stdout and restore it when you are done.
Expand|Select|Wrap|Line Numbers
  1. stdout_old = sys.stdout
  2. sys.stdout = open("test.txt", 'a')
  3. # do stuff
  4. sys.stdout = stdout_old
Sep 8 '14 #4
bvdet
2,851 Expert Mod 2GB
Not that I know of. Information is stored on disc in files. A directory is not a file. BTW, File objects don't have to have extensions.
Sep 8 '14 #5
dwblas
626 Expert 512MB
If you are running linux you can pipe standard output to a file directly

program_name.py > output_print_file.txt
Sep 8 '14 #6
So I tried the sysstd output with this code
Expand|Select|Wrap|Line Numbers
  1. import os
  2.  
  3. path = '/Users/felishalawrence/testswps/vol1'  
  4. for file in os.listdir(path):
  5.         newFile = file[:file.rindex("v")]+"v20"
  6. sys.stdout =  '/Users/felishalawrence/testswps/vol1'
  7. print newFile
and got nothing. No errors and it did not print out my results in ipython notebook when I removed line 5 and ran it again. The output was also not in the folder specified. What did I do wrong? Can I reset it? If so, how?
Sep 9 '14 #7
bvdet
2,851 Expert Mod 2GB
In your for loop, you define a value to identifier "newfile", but you perform no action with this value. After the for loop, you assign sys.stdout to a directory when it should be assigned to an open file object. The following print statement goes nowhere.
Sep 15 '14 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Xah Lee | last post by:
suppose you want to do find & replace of string of all files in a directory. here's the code: ©# -*- coding: utf-8 -*- ©# Python © ©import os,sys © ©mydir= '/Users/t/web'
2
by: Bill Sneddon | last post by:
Can any one tell me how to output the following string? <%response.write "<tr><td><a href=""file://SERVER/mmlogs/TNAME" & yearmonth & """>"& "MYJUNK" & "</a><BR></td></tr>" %> ...
8
by: nick | last post by:
I have a problem and I've been using a cheezy work around and was wondering if anyone else out there has a better solution. The problem: Let's say I have a web application appA. Locally, I set...
0
by: floele | last post by:
Hi. I try to create a small program to handle the FLAC <http://flac.sourceforge.net/> (commandline) encoder or decoder and intend to read its command line output during the process like that: ...
11
by: FNX | last post by:
I'm stumbling around a little bit here trying to get a feel for wide/Unicode functionality. I'm pretty up on C++ in general, streaming stuff not so much. If I run the following code, I get a...
9
by: Diane | last post by:
Could you please explain me how can I output nested strings? Here is an example: "adsd{rfkm}xcv" The output should start from the inner parentheses, such as: dfF rfkm
7
by: Lee | last post by:
Hi, I'm a stream virgin and am attempting to output strings to a file. My approach is to write the string initially to a 'stringstream' and only when complete write the stringstream to the file...
12
by: romayankin | last post by:
I plan to write site on a few language. For this reason I'm trying to find the most convenient way to output strings depending on their unique ID. As far as I plan to have pretty much strings...
0
by: flameboy | last post by:
I am currently trying to figure my way how to output strings to a file. I have seen all the many posts and such how to do it in a console program, and have had much success doing it that way. This...
6
by: Jojo | last post by:
Hi all, I was wondering how I can perform formatted output with C++ strings. For example, suppose I have in plain C: sprintf(C_string, "%5.2f %6d"); How can I do such a thing with C++...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.