writing to a file in python

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • texas22
    New Member
    • Jun 2007
    • 26

    writing to a file in python

    How do I create a directory and then within that directory create a file for every day of the year so they will be named 0101, 0102 and so on. Also what do I need to do so I can put some basic html code in each file lets say for now I want the files to just have <html> <body> </html> in each file for the 365 days in the year. Is there a quick way in python to do this
  • William Manley
    New Member
    • Mar 2007
    • 56

    #2
    [CODE=python]mypath = 'c:\\year' #Your directory here
    import os #import the operating system module
    os.mkdir(mypath ) #make the directory
    for day in range(1,366): #make a for loop for every day in the year, use 366 instead of 365 - 366 won't be included.
    f = open(mypath + '\\' + str(day) + '.html','w') # open the file
    f.write('<html> \n<body>\n</body>\n</html>') #write the data to the file. the \n is a newline character.
    f.close() #close the file
    print 'Done!' # We're done![/CODE]

    I'm pretty sure this is what you want. Yay! I was able to contribute back to this community finally. :)
    Last edited by William Manley; Jun 13 '07, 06:49 AM. Reason: typo

    Comment

    • ghostdog74
      Recognized Expert Contributor
      • Apr 2006
      • 511

      #3
      Code:
      import time,os
      month,day=time.strftime("%m %d",time.localtime()).split()
      dir2create=os.path.join("/home/somedir2create")
      os.mkdir(dir2create)
      os.chdir(dir2create)
      astring = """
      <html>
      <body>
      Blah Blah
      </html>
      """
      open("%s%s"%(month,day),"a").write(astring)

      Comment

      • grantstech
        New Member
        • Jun 2007
        • 16

        #4
        Originally posted by William Manley
        [CODE=python]mypath = 'c:\\year' #Your directory here
        import os #import the operating system module
        os.mkdir(mypath ) #make the directory
        for day in range(1,366): #make a for loop for every day in the year, use 366 instead of 365 - 366 won't be included.
        f = open(mypath + '\\' + str(day) + '.html','w') # open the file
        f.write('<html> \n<body>\n</body>\n</html>') #write the data to the file. the \n is a newline character.
        f.close() #close the file
        print 'Done!' # We're done![/CODE]

        I'm pretty sure this is what you want. Yay! I was able to contribute back to this community finally. :)

        This is awesome. Now, how would I change this so that the files would be named in month,day format like 0101 for January 1st.?

        Comment

        • bvdet
          Recognized Expert Specialist
          • Oct 2006
          • 2851

          #5
          Originally posted by grantstech
          This is awesome. Now, how would I change this so that the files would be named in month,day format like 0101 for January 1st.?
          This is one way to create a file list using month/date:
          Code:
          months = range(1,13)
          days = [31,28,31,30,31,30,31,31,30,31,30,31]
          fileList = ['%02d%02d.txt' % (m,i) for m, no_days in zip(months,days) for i in range(1,no_days+1)]
          >>> for i in fileList:
          ... print i
          ...
          0101.txt
          0102.txt
          0103.txt
          0104.txt
          0105.txt
          0106.txt
          ............
          0128.txt
          0129.txt
          0130.txt
          0131.txt
          0201.txt
          0202.txt
          0203.txt
          ............

          Comment

          • grantstech
            New Member
            • Jun 2007
            • 16

            #6
            Thank you, thats a big help.


            Originally posted by bvdet
            This is one way to create a file list using month/date:
            Code:
            months = range(1,13)
            days = [31,28,31,30,31,30,31,31,30,31,30,31]
            fileList = ['%02d%02d.txt' % (m,i) for m, no_days in zip(months,days) for i in range(1,no_days+1)]
            >>> for i in fileList:
            ... print i
            ...
            0101.txt
            0102.txt
            0103.txt
            0104.txt
            0105.txt
            0106.txt
            ............
            0128.txt
            0129.txt
            0130.txt
            0131.txt
            0201.txt
            0202.txt
            0203.txt
            ............

            Comment

            • texas22
              New Member
              • Jun 2007
              • 26

              #7
              Ok I was able to make a new directory then put the files in there for all 365 days in year 0101.txt, 0102, and so on now what do I need to do to tell it to open each file and write the same thing to each file, they are all going to be html files so I want to put just <html><body></body></html> for now so I can add to each later.
              Thanks for all the help

              Comment

              • texas22
                New Member
                • Jun 2007
                • 26

                #8
                Originally posted by bvdet
                This is one way to create a file list using month/date:
                Code:
                months = range(1,13)
                days = [31,28,31,30,31,30,31,31,30,31,30,31]
                fileList = ['%02d%02d.txt' % (m,i) for m, no_days in zip(months,days) for i in range(1,no_days+1)]
                >>> for i in fileList:
                ... print i
                ...
                0101.txt
                0102.txt
                0103.txt
                0104.txt
                0105.txt
                0106.txt
                ............
                0128.txt
                0129.txt
                0130.txt
                0131.txt
                0201.txt
                0202.txt
                0203.txt
                ............
                Ok I was able to make a new directory then put the files in there for all 365 days in year 0101.txt, 0102, and so on now what do I need to do to tell it to open each file and write the same thing to each file, they are all going to be html files so I want to put just <html><body></body></html> for now so I can add to each later.
                Thanks for all the help

                Comment

                • bartonc
                  Recognized Expert Expert
                  • Sep 2006
                  • 6478

                  #9
                  Originally posted by texas22
                  Ok I was able to make a new directory then put the files in there for all 365 days in year 0101.txt, 0102, and so on now what do I need to do to tell it to open each file and write the same thing to each file, they are all going to be html files so I want to put just <html><body></body></html> for now so I can add to each later.
                  Thanks for all the help
                  Once you have a list of file names, as in:
                  Code:
                  fileList = ['%02d%02d.txt' % (m,i) for m, no_days in zip(months,days) for i in range(1,no_days+1)]
                  you can iterate through that like this:[CODE=python]for fName in fileList:
                  f = open(fName, 'w')
                  f.write("Don't forget the/nnewline character for multiline text")
                  f.close()[/CODE]

                  Comment

                  • texas22
                    New Member
                    • Jun 2007
                    • 26

                    #10
                    Originally posted by bartonc
                    Once you have a list of file names, as in:
                    Code:
                    fileList = ['%02d%02d.txt' % (m,i) for m, no_days in zip(months,days) for i in range(1,no_days+1)]
                    you can iterate through that like this:[CODE=python]for fName in fileList:
                    f = open(fName, 'w')
                    f.write("Don't forget the/nnewline character for multiline text")
                    f.close()[/CODE]
                    Awesome thanks,

                    So I was able to get it to work but it made all those 300 something files and put them on my C/drive how do I tell it to put all the files in the directory files that I created

                    Comment

                    • texas22
                      New Member
                      • Jun 2007
                      • 26

                      #11
                      Pretty sure I got it Thanks for all the help guys

                      Comment

                      • texas22
                        New Member
                        • Jun 2007
                        • 26

                        #12
                        writing to a file in python

                        Ok guys, I have asked about this in another discussion but here it goes if I have an html file for everyday of the year such as 0101 for January 1st 0102 and so on and I want to write to it so that each file will have <html><body>0 1/01(pertaining to the name of the file then it file 0102 it would have the date 01/02 so pretty much I'm what I need to do is for every file I need for it to have the date that pertains to that file so it should look something like

                        Python
                        (code)
                        f.write("<html> \n<body>\n01/02<hr>\n</body>\n</html&html")

                        but for the first file the 01/02 needs to be 01/01 to match the file name then file two which is 0102 needs to have the date 01/02 to match the name of that file name
                        Is this making any sense??

                        Comment

                        • bartonc
                          Recognized Expert Expert
                          • Sep 2006
                          • 6478

                          #13
                          Originally posted by texas22
                          Ok guys, I have asked about this in another discussion but here it goes if I have an html file for everyday of the year such as 0101 for January 1st 0102 and so on and I want to write to it so that each file will have <html><body>0 1/01(pertaining to the name of the file then it file 0102 it would have the date 01/02 so pretty much I'm what I need to do is for every file I need for it to have the date that pertains to that file so it should look something like

                          Python
                          (code)
                          f.write("<html> \n<body>\n01/02<hr>\n</body>\n</html&html")

                          but for the first file the 01/02 needs to be 01/01 to match the file name then file two which is 0102 needs to have the date 01/02 to match the name of that file name
                          Is this making any sense??
                          Something like this, perhaps?
                          [CODE=python]
                          >>> for i in range(1,13):
                          ... fileName = r"thisisapath\0 1%02d" %i
                          ... htmlLine = "<html>\n<body> \n01/%02d<hr>\n</body>\n</html&html" %i
                          ... print fileName
                          ... print repr(htmlLine)
                          ... [/CODE]
                          thisisapath\010 1
                          '<html>\n<body> \n01/01<hr>\n</body>\n</html&html'
                          thisisapath\010 2
                          '<html>\n<body> \n01/02<hr>\n</body>\n</html&html'
                          thisisapath\010 3
                          '<html>\n<body> \n01/03<hr>\n</body>\n</html&html'
                          thisisapath\010 4
                          '<html>\n<body> \n01/04<hr>\n</body>\n</html&html'
                          thisisapath\010 5
                          '<html>\n<body> \n01/05<hr>\n</body>\n</html&html'
                          thisisapath\010 6
                          '<html>\n<body> \n01/06<hr>\n</body>\n</html&html'
                          thisisapath\010 7
                          '<html>\n<body> \n01/07<hr>\n</body>\n</html&html'
                          thisisapath\010 8
                          '<html>\n<body> \n01/08<hr>\n</body>\n</html&html'
                          thisisapath\010 9
                          '<html>\n<body> \n01/09<hr>\n</body>\n</html&html'
                          thisisapath\011 0
                          '<html>\n<body> \n01/10<hr>\n</body>\n</html&html'
                          thisisapath\011 1
                          '<html>\n<body> \n01/11<hr>\n</body>\n</html&html'
                          thisisapath\011 2
                          '<html>\n<body> \n01/12<hr>\n</body>\n</html&html'
                          >>>

                          Comment

                          • bartonc
                            Recognized Expert Expert
                            • Sep 2006
                            • 6478

                            #14
                            Originally posted by texas22
                            Ok guys, I have asked about this in another discussion <snip>
                            If you need to know how to find your old posts, we can teach you.
                            For now, it's not a lot of trouble for me to do it, but you should learn the workings of the site sooner than later.

                            Thanks.

                            Comment

                            • akbkgp
                              New Member
                              • Sep 2007
                              • 2

                              #15
                              Originally posted by ghostdog74
                              Code:
                              import time,os
                              month,day=time.strftime("%m %d",time.localtime()).split()
                              dir2create=os.path.join("/home/somedir2create")
                              os.mkdir(dir2create)
                              os.chdir(dir2create)
                              astring = """
                              <html>
                              <body>
                              Blah Blah
                              </html>
                              """
                              open("%s%s"%(month,day),"a").write(astring)
                              Thanks for the script, I almost succeed to write a similar m file for my python code. Now I'm having a problem, suppose I want to write the file with some specific no.s/characters previously defined in .py file. str() doesn't help me feeding the data to the m file to be written, what's the procedure ? Thanks.

                              Comment

                              Working...