Can someone help please

G

Gary

Hi
Can someone help me with this code below please,
For some reason it will not send me the first text file in the directory.
I made up an empty file a.txt file with nothing on it and it sends the
files i need but would like to fix the code.
Thanks




total = ' '
os.chdir('/home/woodygar/Desktop/Docs')
for i in os.listdir('.'):
if '.txt' in i:
f = open(i, 'r')
total += f.read()
f.close()
message = """\
Subject: %s
%s

"""% (SUBJECT,total)
 
W

woooee

For some reason it will not send me the first text file in the directory.

You have to print an unsorted list of the directory to know the name
or the first file in the directory. Files are not stored on disk in
alphabetical order, but are many times sorted in alphabetical name
order by a program that lists the directory. Note that if you want
the first file alphabetically, you can sort the list returned by
listdir() and then look for the first ".txt" file.
 
B

Billy Mays

Hi
Can someone help me with this code below please,
For some reason it will not send me the first text file in the directory.
I made up an empty file a.txt file with nothing on it and it sends the
files i need but would like to fix the code.
Thanks




total = ' '
os.chdir('/home/woodygar/Desktop/Docs')
for i in os.listdir('.'):
if '.txt' in i:
f = open(i, 'r')
total += f.read()
f.close()
message = """\
Subject: %s
%s

"""% (SUBJECT,total)

Does the file end with '.TXT' ? This might help:

total = ' '
os.chdir('/home/woodygar/Desktop/Docs')
txts = (nm for nm in os.listdir('.') if nm.lower().endswith('.txt') )
for nm in txts:
f = open(nm, 'r')
total += f.readlines()
f.close()
message = """\
Subject: %s
%s

"""% (SUBJECT,total)




I also changed read to readlines().
 
G

Gary Herron

Does the file end with '.TXT' ? This might help:

total = ' '
os.chdir('/home/woodygar/Desktop/Docs')
txts = (nm for nm in os.listdir('.') if nm.lower().endswith('.txt') )
for nm in txts:
f = open(nm, 'r')
total += f.readlines()
f.close()
message = """\
Subject: %s
%s

"""% (SUBJECT,total)




I also changed read to readlines().
That won't work (You must not have even tried to run this.) The call
f.readlines() returns a list which causes an error when added to a string:

TypeError: cannot concatenate 'str' and 'list' objects


Gary Herron
 
B

Billy Mays

That won't work (You must not have even tried to run this.) The call
f.readlines() returns a list which causes an error when added to a string:

TypeError: cannot concatenate 'str' and 'list' objects


Gary Herron

You're right, I didn't. But thats not really the important part of the
code. I believe the generator should work, (and in fact, should
probably use os.path.walk instead of os.listdir )
 
D

D'Arcy J.M. Cain

Hi
Thanks for your reply's
and sorry guys for not explaining properly
ok the problem with the code, which i never realised before, is it sends the
first txt file as the header or subject field in an email and the rest in
the body of the email which i don't want. I would like all the txt files in
the body of an email

Change this to:
total = '\n'

You just need a blank line between the headers and the body of the email
message.
 
R

rantingrick

--------------------------------------------------
total = ' '
os.chdir('/home/woodygar/Desktop/Docs')
for i in os.listdir('.'):
--------------------------------------------------
"i" was a bad local var choice here! i and x are typically reserved to
represent integer indexes in local loop scope whilst iterating over a
container object. Likewise y and z are meant to represent the next
levels of indexes. However when looping over a list of strings use
something more appropriate like:

for filename in os.listdir('.'):
do_something_with(filename)

--------------------------------------------------
   if '.txt' in i:
--------------------------------------------------

No, no, don't do that! Do if str.endswith('.txt') instead. Or use the
os.path methods.

--------------------------------------------------
     f = open(i, 'r')
     total += f.read()
     f.close()
--------------------------------------------------

Two bad things happening here;

(1.) Need to catch exceptions.

try:
f = open(i, 'r')
total += f.read()
except IOERROR:
freak_out()
else:
f.close()

(2.) NEVER concatenate a string with += in a loop! Instead load the
strings into a list and then use ''.join(lst) method on the list.
This is line one
this is line two

this is line four\
"""
lst = []
for line in filetext.splitlines(): lst.append(line)
lst ['This is line one', 'this is line two', '', 'this is line four']
'\n'.join(lst) 'This is line one\nthis is line two\n\nthis is line four'
print '\n'.join(lst)
This is line one
this is line two

this is line four
--------------------------------------------------
message = """\
Subject: %s
%s

"""% (SUBJECT,total)
--------------------------------------------------

Use the new format spec over the old string interpolation if available
in your version of python.

MSG = """
Subject: {0}
{1}
"""
MSG.format(SUBJECT,total)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top