当前位置:网站首页>Slashes / and backslashes involved in writing code\

Slashes / and backslashes involved in writing code\

2022-07-27 05:03:00 Inexhaustible hand of God

Writing code involves slashes / And the backslash \

 I remember when I was in college, what I hated most was the slash (/) And the backslash (\), Often foolishness can't tell , This super attacks people's coding enthusiasm .

1.

 But with reading some code books , Namely 《Python From introduction to practice 》, I see , Writing code is a process of trial and error , When you write code, it's 100% wrong , Now I don't care about the difference between slash and backslash , Anyway, write it in (IDE), Integrated development environment ( Like PyCharm) Will report a mistake , Execution can also quickly see the results .

 But we still have to distinguish it , We use it a lot Windows The path in the system is the backslash (\), and Linux The system is a slash (/).

2.

 So let's look at the code (Python Under the code ):
 Relative paths (Windows operating system ):
with open("text_file\digits_1.txt") as file_object:
    contents = file_object.read()
    print(contents)

 Relative paths (Linux operating system ):
with open('text_files/filename.txt') as file_object:

 Absolute path path (Windows operating system ):
path = "F:\python_project\other_text_file\other_digits_1.txt"

with open(path) as file_object:
    contents = file_object.read()
    print(contents)

 Absolute path path (Linux operating system ):
file_path ='/home/ehmatthes/other_files/text_files/filename.txt' 
with open(file_path) as file_object:

 Be careful  Windows Sometimes the system can correctly interpret the slash in the file path . If you're using Windows System , And the results do not meet expectations , Make sure you use a backslash in the file path .

 Relative paths (Windows operating system ):
with open("text_file/digits_1.txt") as file_object:
    contents=file_object.read()
    print(contents)


 Reverse diagonal and backslash , Try it yourself , If this doesn't work, change that .

3.

 But I understand a little bit :
	 Any programmer will encounter problems , This is inevitable 
	 Program error , Read the log information carefully , Error message 
	 Leave the computer , Let's take a rest , Try again .

 Anyway, you write code , Write it out and run it first ! by the way ,Python Very elegant! , The code structure is very clear , Learn it , You'll see , Compare ” Elegance never goes out of style !“
原网站

版权声明
本文为[Inexhaustible hand of God]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/208/202207270441548376.html