r/pythonhelp

Reading First Few Bytes of Every File on a Drive

Hi! Hoping someone can help. My Python programing is a bit rusty.

I am having an issue where my computer is replacing file data with empty data after a cut/paste or a save. I’m not sure of the cause — my primary objective at the moment is quantifying the damage and protecting my files.

The files look normal in Windows Explorer, except they will not open like they should. Upon opening in NotePad, it is revealed that all data in the file has been completely replaced with what appears to be whitespace, resulting in an empty file that appears to be the same size as the original. I cannot possibly test and check every file manually.

What I would like help with is writing a Python script that cycles through every file on a drive to pull the first several bytes from each file and compare those bytes to a string of the aforementioned whitespace. If there is a match, add the file path to a running list in a *.txt file.

I don’t think this is an especially complex script, but, as I said earlier, I’m pretty rusty. With a solid start, I can get it the rest of the way to doing what I want.

Any help would be appreciated. 🙂

reddit.com
u/hiplobonoxa — 11 days ago

hi, i have very limited knowledge of coding, but I'm trying to install something from github and i've been able to figure out most of it, but im stuck on this step.

i've manually installed python for windows 11 and i've successfully installed pip3 with windows powershell, but despite doing this, when i go to git bash to paste in the next line of code I need, i get the error "bash: pip3: command not found"

i even went through the advanced settings on windows to add the correct subfolder of pip3 to PATH, but git bash is still giving me the same error message.

sorry if this isn't enough information to resolve my issue, i'll try to be active and respond to any follow-up questions people have. im honestly just not sure what else to do to fix things.

reddit.com
u/Haandotexe — 14 days ago
▲ 1 r/pythonhelp+2 crossposts

Got back into Python after a break and quickly realized I did NOT want to sit through long tutorials or docs again just to remember basic stuff.

So I vibe-coded a simple Python cheat sheet site.

Built it for myself, but thought it might help others too.

check it out, would love any feedback or ideas on what I should add next
https://hawks6.github.io/Python-Cheat-Sheet/

https://github.com/Hawks6/Python-Cheat-Sheet - free to check out the repo and contribute.

reddit.com
u/Neat-Suggestion9825 — 9 days ago

string.translate(str.maketrans) not working to take out punctuation?

greeting = ["Hello, my name is Caine! What's your name?: ".upper(), "Welcome, I am Caine! What's your name?: ".upper(),
        "Hi!! I'm Caine! What should I call you?: ".upper()]


name = input(random.choice(greeting)).title()


nameStripped = name.translate(str.maketrans('', '', string.punctuation))


for i in nameList:
    if i in nameStripped:
        userName = i 


justNameWelcome = [f"Nice to meet you, {userName}!".upper(), f"{userName}... That's a wonderful name!".upper(),
              f"I've always thought that {userName} is a good name!".upper()]
ntmyNameWelcome = [f"It's nice to meet you too, {userName}!".upper(), f"The sentiment is mutual, {userName}!".upper(),
                f"I appreciate the sentiment, {userName}!".upper()]
hruNameWelcome = [f"I'm doing well, {userName}! Thank you for asking!".upper(), f"I appreciate you asking! Thank you, {userName}, I'm doing well!".upper(),]


niceToMeetYou = ["Nice To Meet You", "Pleasure To Meet You"]
howAreYou = ["How Are You", "How Are You Doing", "Are You Well"]


if len(name.split()) > 1:
    if nameStripped in howAreYou:
        print(random.choice(hruNameWelcome))
    elif nameStripped in niceToMeetYou:
        print(random.choice(ntmyNameWelcome))
    else:
        print(random.choice(misunderstandings))


elif len(name.split()) == 1:
    print(random.choice(justNameWelcome))


else:
    print(random.choice(blankInputs))
    nameStripped = input(random.choice(greeting)).title()

Thank you in advance, I've been stuck here for about two days now,,

I've copy and pasted the suggested way of taking out the punctuation in the name string, that way it can recognize certain phrases in the other lists. Right now, as its only at the very start, I only have it set to notice if the beginning output has either a phrase akin to "How are you" or "Nice to meet you". These are both set in lists, and the if-else statement checks to see if the name input has more than one word, and will check for the phrasing in there. It's not registering that the punctuation has been removed, even when setting the new string to a new variable.

IE:

input Hi! I'm Jon! How are you?

output: I'm doing well, Jon!

Instead it gives one of the misunderstanding outputs. It works fine if I only use one word with ending punctuation, but it seems like if there is more than one word or punctuation in the middle, it can't remove it.

Is it in the wrong spot, or am I using the wrong method for this situation?

reddit.com
u/duelBooleans — 9 days ago

Why doesn't it execute when creating Python or Core Lab commands with Geminai?

I wrote the code with Geminai, but it doesn't run in CoLab. Does using AI mean the code won't run?

and I put the code I wrote on GitHub into Geminai, and I also ran it on Corep, you know? I don't know what the reason is. Sorry for the long post!

reddit.com
u/Special_Ear6817 — 7 days ago

Calling an assembly instruction in Python

Long story short, i want to make THE most horrible python calculator to ever exist. For that i need a way to call an assembly instruction directly in my python script.

I know you can do that in C with inline assembly, and i know CFFI exists and allows calling C functions in python, so i tried to use that. However CFFI's parser rejected __asm__ syntax and threw an error because inline assembly isn't standard C apparently.

Is there some sort of a workaround to call an assembly instruction in python script? It doesn't have to be clean, in fact, it's better if it's absolutely terrible, bonus points for unsafe

reddit.com
u/i_walk_away — 4 days ago

Appending the last line of a dataframe to a csv file is giving weird formatting.

What I am trying to do:

  1. Check if a file exists. Set a boolean
  2. Create a dataframe that either reads in the file or is just created with headers only
  3. Once I calculate some stuff, the boolean from step 1 is checked. If this is the first term of the dataframe, the entire dataframe gets saved to a csv.

````df.to_csv(dataFileName + ".csv", index = False,float_format="{:.2f}".format)

So now I have a header row and 1 row of data. This part works as intended.

If this is a pre-existing file, I only want to append the new term onto the end of the file. I use this:

````df.iloc[len(df)-1].to_csv(DFN + ".csv", index = False, mode = 'a',float_format="{:.2f}".format)

I get some weird formatting where each term in a row gets its own row.

https://i.imgur.com/HjoXAYC.png

My assumption is appending the file is quicker than reading in the whole file, wiping it, and writing all the data.

I mainly want to do this as a backup so I can save data mid-calculation and have something to look at if things break. After the whole thing is finished, I sort the dataframe, rename the file I've been working with to be a backup, and then finally write the complete, sorted dataframe to file. If something bad happens during writing this file, the backup file should have the same data already, just not sorted.

Thanks!

u/Preeng — 3 days ago