Linux - GeneralThis Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I am reading a database and output data em CSV format to a data migration job.
I have to create 3 separate outputs each one in a different format.
My first approach was creating 3 python programs, each one outputing data in the desired format, but I realize that I have to read the same data from database in each run.
So I was wondering if I can do something similar to this:
To expand on what I think Michael is getting at, you can pass the the filenames into the script and open them with Python, then replace your current stdout/print calls with the relevant file handlers.
You all are right , opening to write several files is more pythonic and the way I was trying to achieve using multiple descriptors is more "bashish"...
I will do in that way.
But, just to be clear, conceptually speaking, in linux, a program can have only 2 output streams ? Only stdout and stderr ? It is not possible to have more than 2 ?
I mean, is there a way to a program can be called as "a_program >file_1 2>file_2 3>file_3 4>file_4" ?
How I could use "with" with 4 files, as it is the case ?
Code:
with open('file_1', 'w') as f1:
with open('file_2', 'w') as f2:
with open('file_3', 'w') as f3:
with open('file_4', 'w') as f4:
f1.write('file contents\n')
f2.write('file contents\n')
f3.write('file contents\n')
f4.write('file contents\n')
#!/usr/bin/python
a = "Some text goes here"
b = "A,B,C"
c = "color=red"
d = "A|big|brown|dog"
with open('file1.txt', 'w') as f:
f.write(a)
with open('file1.csv', 'w') as g:
g.write(b)
with open('file1.conf', 'w') as h:
h.write(c)
with open('file2.csv', 'w') as i:
i.write(d)
Edit:
Also
Code:
#!/usr/bin/python
a = "Line 1"
b = "Line 2"
c = "Line 3"
d = "Line 4"
for x in a,b,c,d:
with open('file3.txt', 'a') as f:
f.write(x + '\n')
Not quite....
There is no a single print to each file.
Instead I read a db, collect the data (let's say it is something like "field_A; field_B; field_C; field_D; field_F") and output a mix of thsi fields:
file_1 -> "field_a; field_b"
file_2 -> "field_a; field_c; field_f"
file_3 -> "field_a; field_d"
file_4 -> "field_a; field_e; field_f"
Code:
sql= "select field_a, field_b, field_c, field_d, field_e from table where whatever"
cursor.execute (sql)
with open('file_1', 'w') as f1:
with open('file_2', 'w') as f2:
with open('file_3', 'w') as f3:
with open('file_4', 'w') as f4:
for field_a, field_b, field_c, field_d, field_e in cursor:
<the output to the several files goes here>
too many identations levels, isn't ?
Instead,
Code:
f1= open('file_1', 'w')
f2= open('file_2', 'w')
f3= open('file_3', 'w')
f4= open('file_4', 'w')
sql= "select field_a, field_b, field_c, many_more_fields from table where <complex condition goes here>"
cursor.execute (sql)
for field_a, field_b, field_c, many_more_fields in cursor:
<the output to the several fields and files goes here>
close (f1)
close (f2)
close (f3)
close (f4)
I think that in this case, explicit close at end of block it is more readable.
print("Hello world") ~= print"hello world", file=sys.stdout) opens prints to stdout
print("error message", file=sys.stderr) obviously opens and prints to stderr
I think if you want to write to a file descriptor that isn't 1 or 2 would need to open it with os.fdopen(x,args).
f3 = os.fdopen(3, "w+")
f3.write("something else")
Since this is a short-lived script, Python should close the files automatically when the script finishes.
(If it were part of a persistent application, that's when you need to worry about either using "with" or explicitly closing - if the latter, you also need suitable exception handling (e.g. "try/finally") to ensure it gets closed if the unexpected occurs.)
In any case, if you're looping through a resultset, instead of writing to each file on every loop iteration, consider storing the results in an array per file, then writing that to file once at the end. Quick example...
Code:
data1 = []
data2 = []
for field_a, field_b, field_c, field_d, field_e in cursor:
data1.append(f"{field_a}; {field_b}")
data2.append(f"{field_a}; {field_c},{field_f})")
with open('file_1','w') as f1:
f1.write('\n'.join(data1))
with open('file_2','w') as f1:
f2.write('\n'.join(data2))
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.