-1

I have googled and googled and I can't find anything that works. I need to grab the date within the name of the file, turn it into the julian date, and then, rename the original file so that it also includes the julian date.

for file in os.listdir("/home/mydir/"):  
    if file.startswith("awesome"):  
        awesome_file = file.split(' ')  
        awesome_date = awesome_file[2]  
        awesome_year = awesome_date.split()  
        awesome_year = awesome_date[0] + awesome_date[1] + awesome_date[2] + awesome_date[3]  
        awesome_month = awesome_date[4] + awesome_date [5]  
        awesome_day = awesome_date[6] + awesome_date[7]  
        date_command = "date -d " + awesome_month + "/" + awesome_day + "/" + awesome_year + " +%Y%j"  
        print(date_command)  
        julian_date = subprocess.Popen(date_command)  
        print(julian_date)

I know that the problem is within the subprocess.Popen line, but as you can see, I have to run the actual bash command as a variable since the command will likely differ for every file that is found within /home/mydir/ and I can't figure out the correct syntax to save my life. Also, just as reminder, I also need to capture the output of the subprocess.Popen because I need the STDOUT to rename the original awesome_file with.

00mpa
  • 1
  • What happens when this code runs currently? What do you get for `date_command` and `julian_date`? – Etan Reisner Jun 05 '15 at 18:57
  • print(date_command) outputs "date -d 05/28/2015 +%Y%j", and if I use subprocess.call instead of subprocess.Popen, it prints to screen "2015148", but print(julian_date) ouputs "0". – 00mpa Jun 05 '15 at 19:00
  • http://stackoverflow.com/questions/2502833/store-output-of-subprocess-popen-call-in-a-string ? – Etan Reisner Jun 05 '15 at 19:04
  • That doesn't work because it doesn't like the fact that I pass a variable to subprocess.Popen instead of the actual command arguments. – 00mpa Jun 05 '15 at 19:05
  • `subprocess.Popen` returns a `Popen` object. You should use `subprocess.check_output`. Also, you might need to `.decode("utf-8")`. – 4ae1e1 Jun 05 '15 at 19:05
  • 1
    There are other problems with this script; e.g, you should use slicing rather than adding characters one by one (`awesome_date[:3]` etc.). Also, I would personally use `re.sub` for transforming the string, or use a dedicated date formatting library, e.g., `arrow`. – 4ae1e1 Jun 05 '15 at 19:08
  • When I try running subprocess.check_output I get a "AttributeError: 'module' object has no attribute 'check_output'". I am new to python so I'm wasn't familiar with slicing. I'm googling it now though. – 00mpa Jun 05 '15 at 19:09
  • `check_output` is "newer" python unfortunately. But the `stdout`/`PIPE` stuff from the linked question should work just fine. Just don't construct a single string make strings for each argument. – Etan Reisner Jun 05 '15 at 19:10
  • I sliced the awesome_date as suggested and broke up the date_command into four separate strings for each portion of the date_command. It works now. Thanks everyone! – 00mpa Jun 05 '15 at 19:30
  • @00mpa: *"I have googled and googled"* -- I've put the title of your question into google and got a couple SO questions in the results: [Pipe subprocess standard output to a variable](http://stackoverflow.com/q/4514751/4279) and [Running shell command from Python and capturing the output](http://stackoverflow.com/q/4760215/4279). Use the answers and ask a different question if you stuck. – jfs Jun 05 '15 at 19:53
  • There's no reason to use `subprocess` to call `date`; you can construct a `datetime` object and [use the `timetuple` method to get the day of the year](http://stackoverflow.com/a/623312/1126841). – chepner Jun 05 '15 at 20:23

1 Answers1

-1

Instead of calling an external program, use the facilities already available in Python.

from datetime import datetime
d = datetime(int(awesome_year), int(awesome_month), int(awesome_day))
print "{0.tm_year}{0.tm_yday}".format(d.timetuple())
chepner
  • 497,756
  • 71
  • 530
  • 681
  • @J.F.Sebastian, I saw those but my syntax was screwed up so I couldn't get them to work. I wasn't splitting my date command correctly which part of the issue. – 00mpa Jun 06 '15 at 22:08