The below is my code and on executing got the error message: line 1
SyntaxError: EOL while scanning string literal.
Help me in identifying the issue.
code is::
sessions=os.listdir('\Downloads\Rawdata\')
sessions=sessions[3:]
files=[]
for _ in sessions:
dire=os.listdir('\Downloads\Rawdata\')
for __ in dire:
files+=os.listdir('Downloads\Rawdata\'+__)
print(files)
The error is thrown by unescaped backslashes in your path. If you're lucky, your script may run without any issues but I would prefer to use the raw string literal in this case i.e. the 'r'-prefix before your path string. Also, do take note that you should not end your string with a '\' i.e. before the closing quote.
sessions=os.listdir(r'Downloads\Rawdata')
I also noticed that your path strings begin with a '\'. If you plan to navigate relative to your root, just define your path without beginning it with a backslash. r'child_folder\grandchild_folder'
Further reading
Related
I'm trying to use a ZShell function to replace forward- and backslashes with a hyphen. So that "Hello/Every\Person" becomes "Hello-Every-Person". I can do it for the forward slash using
arg=${arg:gs/\//-}
but when I try the same syntax for the backslash it fails. Running
arg=${arg:gs/\//-}
arg=${arg:gs/\\/-}
produces "Hello-EveryPerson". It strips out the backslash, but doesn't do the replacement. I was expecting the normal rules of escaping characters to apply, so the double backslash would resolve to a character.
Can anyone tell me what I'm missing?
Works in zsh version 5.8 exactly as you expected.
I wanted to write the file path into a variable so i used the str to do that and got that:
\f inside of the path
and that happens often so how do i solve this issue?
Replace all your \ with \\.
From the Python docs:
The backslash (\) character is used to escape characters that otherwise have a special meaning, such as newline, backslash itself, or the quote character.
I am using express res.download to download the file from the server location as below-
res.download('path', 'report a/b/c.tiff')
on downloading above file name is getting changed to c.tiff.
what will be the possible reason and how to get downloaded file as report a/b/c.tiff only.
/ is a globally user for differentiating directories hence forth OS will not allow / in file names as well as for directory.
In Windows you will get the below error,
likewise linux OS also uses the same. But we can use '\' for creating files. Allowed chars in Linux,
Double your \, like this: \, so that your shell does not interpret the backslashes from your filename as escape characters.
Escape " and ', like this: \", \', so that your shell interprets the double quotes as part of the filename.
Escape $, like this: \$, otherwise your shell will think you're using a variable.
Escape ? and *, like this: \?, *
Hence you download has only c.tff
I'm making a game in pygame and I have images i'm trying to load but it doesn't work unless I set the directory with OS module, and that would be fine. Except if I want to make it an App the directory is hard coded. How could I do this?
I've tried getting the directory and using the replace function to change the slashes to double slashes but it didn't work.
import os
from pathlib import Path
path = Path.cwd()
path1 = path
print(path1)
input()
print(path1.replace("\","\\"))
input("")
When I try running the code I get:
print(path1.replace("\","\\"))
^
SyntaxError: unexpected character after line continuation character
The backslash (\) sign in a string starts an escape sequence. A single back slash sign in a string is \\. See String literals.
To replace a singe \ by \\, it has to be:
print(path1.replace("\\","\\\\"))
I am using the touch command to try and create a file with the name "\?$*'KwaMe'*$?\" (quotation marks included as part of the file name). However when I type touch "\?$*'KwaMe'*$?\" in the Terminal, it doesn't give me the result I am expecting. How can I create this file?
You need to escape special characters with the backslash symbol (\).
This command will create a file named "\?$*'KwaMe'*$?\":
touch \"\\\?\$\*\'KwaMe\'\*\$\?\\\"
Explanation
Double your \, like this: \\, so that your shell does not interpret the backslashes from your filename as escape characters.
Escape " and ', like this: \", \', so that your shell interprets the double quotes as part of the filename.
Escape $, like this: \$, otherwise your shell will think you're using a variable.
Escape ? and *, like this: \?, \*, to prevent filename expansion.