#๐Ÿ”’ help with saving a file

27 messages ยท Page 1 of 1 (latest)

bold mantleBOT
#

@olive spade

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.

remote zephyr
#

add py behind the backticks then it looks like this

elif opts == 10:
    print("Filename: ")
    filename = input("$| ")
    print("Contents: ")
    contents = input("$| ")

    if not filename.endswith(".txt"):
        filename += ".txt"

    file_path = os.path.abspath(filename)

    with open(filename, 'w') as file:
        file.write(contents)

    print(f"Text successfully saved to {file_path}")

which is much more readably

olive spade
#

it didnt work, but for some reason VSCode doesnt let me run scripts so i couldnt see what the error was

remote zephyr
#

but why do you use abspath? it would automatically be stored in the same folder when you just use the filename. and the os module is part of the standard library you do not need to install it, it comes with python

olive spade
#

yes i know i put "import os" at the far beggening of my code

remote zephyr
#

but nevertheless, the code works for me

fleet depot
#

and i mean ur python file

olive spade
#

it tells me error in line 1 invalid sytax

#

this is line 1

#

```import time````

remote zephyr
#

are you sure that the opts == 10 condition is true? because the storing logic works

olive spade
#

yes

#

its an input

remote zephyr
fleet depot
olive spade
#

& C:/Users/user/AppData/Local/Microsoft/WindowsApps/python3.11.exe "c:/Users/user/OneDrive/Desktop/the cool.py"
File "<stdin>", line 1
& C:/Users/user/AppData/Local/Microsoft/WindowsApps/python3.11.exe "c:/Users/user/OneDrive/Desktop/the cool.py"
^
SyntaxError: invalid syntax

remote zephyr
#

there is a & before your path

olive spade
#

how do i remove that?

remote zephyr
#

i do not know how it gets there since i do not have your code

olive spade
#

its alr

#

ill figure i tout

#

!close

bold mantleBOT
#
Python help channel closed with !close

This help channel has been closed. 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.