How do I find and set the directory? - python-3.x

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("\\","\\\\"))

Related

While passing address in nodejs, when to place '/' between directories and when to place '\\' . And why do we place dot(.) before writing addresses

While passing address in nodejs, when to place '/' between directories and when to place '\' . And why do we place dot(.) before writing addresses
path package provides functionality so that you don't need to worry about what directory separator you need to use.
const path = require('path');
path.join(parentDir, childDir);
And dot represents current directory i.e.
project index file path: /home/user/projects/project-name/index.js
you are in <project-name> folder
so index file path will be ./index.js
In a Unix/Linux environments you place a forward slash as a folder seperator.
e.g. /home/user/image.jpg
In a Windows system you use backslashes as folder seperators, since a backslash is also an escape character in a string you should escape the backslash.
e.g. C:\users\user\image.jpg
You use a . for relative paths.
e.g. if your script runs in /home/user/ and you want to access image.jpg you can do so by ./image.jpg
for more info: https://nodejs.org/api/path.html

String commands in the file path

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.

res.downalod changing the name of .tiff file while downloading if fileName contain forward slash

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

python syntax errorEOL while scanning string literal

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

how can i make '\u' work in variable in python3

I got a string from network like:
s = '\u0070\u0079\u0074\u0068\u006f\u006e' or other
when i print(s)
it output '\u0070\u0079\u0074\u0068\u006f\u006e'
I want make '\u' work
when I print(s)
it output 'python' (u'\u0070\u0079\u0074\u0068\u006f\u006e'=python)
what should I do?
I don't know if I got what you mean, but if you mean you want convert unicode characters with trailing slashes to real unicode characters, you can use this solution.
for python2 : print s.decode('unicode-escape')
for python3 : print(bytes(s, 'ascii').decode('unicode-escape'))

Resources