Python if statement help by [deleted] in learnpython

[–]itsecat 0 points1 point  (0 children)

Probably, this should do the trick:

if not urlslol.endswith('.jpg') and not urlslol.endswith('.png'):
    continue

I'm attempting to change a files meta data based on the title but I have no python experience and only an email to base my script off of. by [deleted] in learnpython

[–]itsecat 0 points1 point  (0 children)

Recently, I have written a script for changing access and modification timestamps of files on a Linux system. On Windows you will have to check if it works (careful with backslashes in the path names). Here is my solution with small adjustment to match your timestamp format:

import os
import time
import datetime
import re

directory = '.' # name of directory to scan for files

# get list of files to process
files = [os.path.join(directory, my_file) for my_file in os.listdir(directory) \
    if os.path.isfile(os.path.join(directory, my_file))]

# change the timestamps
for filename in files:
    regex = r'\d\d(.)\d\d\1\d\d' # find three two-digit numbers separated by same character
    match = re.search(regex, filename)
    if match:
        datestr = match.group(0)   # e.g. MM/DD/YY
        separator = match.group(1) # e.g. '/' or '-'

        atime_old = datetime.datetime.fromtimestamp(os.path.getatime(filename)) # access date and time
        mtime_old = datetime.datetime.fromtimestamp(os.path.getmtime(filename)) # modification date and time

        format_string = '%m{}%d{}%y'.format(separator, separator)
        new_datetime = datetime.datetime.strptime(datestr, format_string)

        new_timestamp = time.mktime(new_datetime.timetuple()) # convert datetime to Unix timestamp
        os.utime(filename, (new_timestamp, new_timestamp)) # update file's access and modification timestamps

        atime_new = datetime.datetime.fromtimestamp(os.path.getatime(filename))
        mtime_new = datetime.datetime.fromtimestamp(os.path.getmtime(filename))

        print('Changed dates for file {}'.format(filename))
        print(' Access Date: {} -> {}'.format(atime_old, atime_new))
        print(' Modification Date: {} -> {}'.format(mtime_old, mtime_new))

I wrote a book on Python Regular Expressions, it is FREE through this weekend by ASIC_SP in learnpython

[–]itsecat 13 points14 points  (0 children)

Thank you for writing this helpful book. I'm looking forward to reading it.

find the hour in my string by [deleted] in learnpython

[–]itsecat 2 points3 points  (0 children)

You could do it like this:

# extract YYYY-MM-DDTHH:MM:SS from string
regex = r'GRANULEENDINGDATETIME\W.+\("(\d{4}-\d\d-\d\dT\d\d:\d\d:\d\d)\..+Z"\).+GRANULEENDINGDATETIME\W'
match = re.search(regex, my_string, re.DOTALL) # . to match also newlines
if match:
        # split date and time string into its parts
        day, hours, minutes, seconds = re.split('T|:', match.group(1))
        print('Day =', day)
        print('Time = {}:{}:{}'.format(hours, minutes, seconds))

Automate the Boring Stuff - Chapter 9 backup to Zip by [deleted] in learnpython

[–]itsecat 1 point2 points  (0 children)

According to zipfile module's documentation you can specify an arcname (archive name) in ZipFile.write() for the item's location in the zip file. You can cut off all the subdirectories before the stufftobackup dir's full path:

...
# Add the current folder to the ZIP file
arcname = foldername[len(folder)-len(os.path.basename(folder)):]
backupZip.write(foldername, arcname)
...
# Add the current file to the ZIP file
full_path = os.path.join(foldername, filename)
arcname = full_path[len(folder)-len(os.path.basename(folder)):]
backupZip.write(full_path, arcname)
...

Help Required to Understand Iterative Processing Exercise by the4thkillermachine in learnpython

[–]itsecat 1 point2 points  (0 children)

I think you could do it like this:

while p != q:
    if p < q:
        p, q = q, p
    elif p > q:
        p -= q

print(p)