#๐Ÿ”’ Wallpaper Switcher

35 messages ยท Page 1 of 1 (latest)

drowsy gull
#

ok so i'm stumped

import random
import subprocess
import os

wallList = []
for line in subprocess.check_output("ls -1 /home/corn/Wallpaper", shell=True).decode().splitlines():
    wallList.append(line.strip())

last = open('last.txt', 'r')
print(last.read())
lastWall = last.read()
last.close()

maxIndex = len(wallList) - 1
sel = random.randint(0, maxIndex)
valid = False
choice = f'/home/corn/Wallpaper/{wallList[sel]}'

while valid is False:
    print(choice)
    print(lastWall)
    if wallList[sel] == lastWall:
        print("nuh uh")
    else:
        valid = True

com = ['feh', '--bg-scale', choice]

command = ""
for item in com:
    command += f'{item} '

print(command.strip())
subprocess.run(com)

last = open('last.txt', 'w')
last.write(choice)
last.close()

trying to make a wallpaper switcher script for bspwm
when the last wallpaper and the current one are equal the if statement doesn't see it
example attached

worn oreBOT
#

@drowsy gull

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

slim ocean
#
last = open('last.txt', 'r')
print(last.read())
lastWall = last.read()
last.close()
tawdry shard
#

maybe there's some extra whitespace on the end of one of them, or something

slim ocean
#

This is a big part of your problem.
And see in your console how you have the empty line being printed? See if you can work out why. That's to do with the bit I posted above.

#

Nah, it is the .read() twice.

tawdry shard
#

oh right

slim ocean
#

the first output line is from: print(last.read())
then the second output line is from: print(choice)
then the third output (empty) line is from: print(lastWall)

So why is the third output None? That's the question, and the answer is around file pointers and using .read() twice.

drowsy gull
#

also yeah

#

okay

slim ocean
#

highlighting text and printing isn't the best debugging in the world. It's not the problem here anyway, but you should learn how to use a debugger to do that properly.

drowsy gull
#

so how do i make lastWall not disappear

slim ocean
#

It doesn't disappear. It never is

drowsy gull
#

so why isn't it

drowsy gull
#

okay but why is it never is

#

??๐Ÿ˜ญ

slim ocean
#

I've already answered, you used .read() twice, the second time being an assignment.

drowsy gull
#

yes

#

so it's assigned

#

therefore it is

#

so why is it not

slim ocean
#

Because you've already read the file. You've consumed it and the file pointer is at the end of the file. There is nothing else to read.

drowsy gull
#

oh

#

why does it work like that

#

that is so lame

slim ocean
#

That's one perspective, the other perspective would be your lack of understanding the technology and how it operates is the blame. ๐Ÿคทโ€โ™‚๏ธ

#

Flip the print and assignment.

#

Assign the read data to the variable, then print the variable.

drowsy gull
#

ok

#

thanks but pls no condescension next time

#

!close

worn oreBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.