-1

I need to save a folder called D:\MyPc if the hostname is MyPc

name_file = open("D:/Others/name.txt" , "r") data = name_file.readline() name_file.close() print(os.path.join("D:\", data)) 

the output is D:
MyPc

4
  • what is the problem ? That is very unclear what you expect Commented Oct 23, 2022 at 18:57
  • What is in data? I'm surprised that "D:\" parsed, but if data is a fully qualified path name, the join won't change it. Commented Oct 23, 2022 at 19:02
  • Instead of opening a file we don't have access to, just initialize data to something that we can test. The code you post will raise a syntax error and I don't see any way that it could produce the output shown. Commented Oct 23, 2022 at 19:11
  • The variable data is not important i just need to group them without space Commented Oct 23, 2022 at 19:25

1 Answer 1

2

To check the hostname: How can I use Python to get the system hostname?

Also, there is an error in your code. Change the last line to have double backslash in string:

print(os.path.join("D:\\", data)) 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.