3
\$\begingroup\$

I have many html files saved to my computer.they have same tags like this: Rpi-Cam-Web-Interface- Page 2 - forum and the page number changes
I want to rename file to page number I use this code:

import re
import os
pattern=re.compile('<title>RPi Cam Web Interface - Page \d*')
for i in os.listdir():
   parser=open(i,'r',encoding='utf-8')
   m=pattern.search(parser.read())
   parser.close()
   os.rename(i,m.group()[35:]+'html')

any better way?

\$\endgroup\$
3
  • 4
    \$\begingroup\$ Did it work? What are you unhappy with? \$\endgroup\$
    – Emily L.
    Commented Sep 23, 2020 at 9:40
  • \$\begingroup\$ My code works fine. but I think maybe my code is beginner and there is a better way to do it. \$\endgroup\$ Commented Sep 23, 2020 at 15:31
  • \$\begingroup\$ And If I'm wrong, many webpages will be corrupted.(more than 250) \$\endgroup\$ Commented Sep 23, 2020 at 15:48

1 Answer 1

0
\$\begingroup\$

Spacing For reasonably professional Python code (or for any Python code you or others may have to look at later - ideally, all code you ever write), it would be a good idea to follow the standard PEP 8 style guide, to improve readability. To start with, it recommends putting spaces between arguments and operators, and using 4 spaces for indentation:

pattern = re.compile('<title>RPi Cam Web Interface - Page \d*')
for i in os.listdir():
    parser = open(i, 'r', encoding='utf-8')
    m = pattern.search(parser.read())
    parser.close()
    os.rename(i, m.group()[35:] + 'html')

Raw string It's a good idea to use raw strings when creating Python patterns so that escape sequences aren't interpreted, and instead the backslashes are treated as regular characters inside the pattern. For this particular pattern you're using, it happens to not be necessary, but if you ever had to change it and match a newline \n or a word boundary \b etc, not having used a raw string would result in the script not running as expected.

In other words, you'd want to do something like:

pattern = re.compile(r'<title>RPi Cam Web Interface - Page \d*')
#                    ^

Pattern I see 2 issues with the pattern:

  • The page number is optional because you're matching with \d* instead of \d+. Using \d+ to match one or more digits would make the intent of the pattern clearer. (\d* matches "zero or more digits")
  • You're manually extracting the page number from the match with m.group()[35:]. Use a capturing group in the pattern instead of having to manually count up indicies:
pattern = re.compile(r'<title>RPi Cam Web Interface - Page( \d+)')
#                                                         ^^^^^^
# ...
    page_number = m.group(1)
\$\endgroup\$
2
  • \$\begingroup\$ thanks it works. but why it would be better if I use raw string? Doesn't raw strings mean that escaping characters are interpreted as regular characters? well ,don't '\d' should be interpreted as escape character? \$\endgroup\$ Commented Sep 24, 2020 at 5:13
  • 1
    \$\begingroup\$ \d is not an escape character in a normal non-raw string, but many other characters are escape characters, like \r, \n, \b, and so on. To use those escape characters, you'll either have to double-escape the backslash in order to use them, or use a raw string. Simply using a raw string every time so you don't have to worry about it is the better choice. \$\endgroup\$ Commented Sep 24, 2020 at 13:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.