Hướng dẫn cwd python - trăn cwd

The

print[Path[__file__].resolve[]]  # /home/skovorodkin/stack/scripts/1.py
4 module, introduced in Python 3.4 [PEP 428 — The pathlib module — object-oriented filesystem paths], makes the path-related experience much much better.

Nội dung chính

  • File scripts/2.py
  • 1. Thư mục hiện hành [current directory] trong Python
  • 1.1. Lấy đường dẫn của current directory trong Python
  • 1.2. Thay đổi current directory trong Python
  • 2. Lấy danh sách tập tin và thư mục con của một thư mục
  • 3. Tạo [create] một thư mục mới trong Python
  • 4. Đổi tên [rename] thư mục hoặc file trong Python
  • 5. Xóa [delete] thư mục hoặc file trong Python

pwd

/home/skovorodkin/stack

tree

.
└── scripts
    ├── 1.py
    └── 2.py

In order to get the current working directory, use

print[Path[__file__].resolve[]]  # /home/skovorodkin/stack/scripts/1.py
5:

from pathlib import Path

print[Path.cwd[]]  # /home/skovorodkin/stack

To get an absolute path to your script file, use the

print[Path[__file__].resolve[]]  # /home/skovorodkin/stack/scripts/1.py
6 method:

print[Path[__file__].resolve[]]  # /home/skovorodkin/stack/scripts/1.py

And to get the path of a directory where your script is located, access

print[Path[__file__].resolve[]]  # /home/skovorodkin/stack/scripts/1.py
7 [it is recommended to call
print[Path[__file__].resolve[]]  # /home/skovorodkin/stack/scripts/1.py
8 before
print[Path[__file__].resolve[]]  # /home/skovorodkin/stack/scripts/1.py
7]:

print[Path[__file__].resolve[].parent]  # /home/skovorodkin/stack/scripts

Remember that

print[Path[__file__].resolve[].parent]  # /home/skovorodkin/stack/scripts
0 is not reliable in some situations: How do I get the path of the current executed file in Python?.

Please note, that

print[Path[__file__].resolve[]]  # /home/skovorodkin/stack/scripts/1.py
5,
print[Path[__file__].resolve[]]  # /home/skovorodkin/stack/scripts/1.py
6 and other
print[Path[__file__].resolve[].parent]  # /home/skovorodkin/stack/scripts
3 methods return path objects [
print[Path[__file__].resolve[].parent]  # /home/skovorodkin/stack/scripts
4 in my case], not strings. In Python 3.4 and 3.5 that caused some pain, because
print[Path[__file__].resolve[].parent]  # /home/skovorodkin/stack/scripts
5 built-in function could only work with string or bytes objects, and did not support
print[Path[__file__].resolve[].parent]  # /home/skovorodkin/stack/scripts
3 objects, so you had to convert
print[Path[__file__].resolve[].parent]  # /home/skovorodkin/stack/scripts
3 objects to strings or use the
print[Path[__file__].resolve[].parent]  # /home/skovorodkin/stack/scripts
8 method, but the latter option required you to change old code:

File scripts/2.py

from pathlib import Path

p = Path[__file__].resolve[]

with p.open[] as f: pass
with open[str[p]] as f: pass
with open[p] as f: pass

print['OK']

1. Thư mục hiện hành [current directory] trong Python

python3.5 scripts/2.py

Traceback [most recent call last]:
  File "scripts/2.py", line 11, in 
    with open[p] as f:
TypeError: invalid file: PosixPath['/home/skovorodkin/stack/scripts/2.py']

1.1. Lấy đường dẫn của current directory trong Python

1.2. Thay đổi current directory trong Python

python3.6 scripts/2.py

OK

2. Lấy danh sách tập tin và thư mục con của một thư mụcos để giúp thao tác với thư mục, tập tin dễ dàng hơn.

Nội dung chính

  • 1. Thư mục hiện hành [current directory] trong Python
  • 1.1. Lấy đường dẫn của current directory trong Python
  • 1.2. Thay đổi current directory trong Python
  • 2. Lấy danh sách tập tin và thư mục con của một thư mục
  • 3. Tạo [create] một thư mục mới trong Python
  • 4. Đổi tên [rename] thư mục hoặc file trong Python
  • 5. Xóa [delete] thư mục hoặc file trong Python

1. Thư mục hiện hành [current directory] trong Python

1.1. Lấy đường dẫn của current directory trong Python

1.2. Thay đổi current directory trong Python

from pathlib import Path

p = Path[__file__].resolve[]

with p.open[] as f: pass
with open[str[p]] as f: pass
with open[p] as f: pass

print['OK']
4 trong module os. Hàm
from pathlib import Path

p = Path[__file__].resolve[]

with p.open[] as f: pass
with open[str[p]] as f: pass
with open[p] as f: pass

print['OK']
4
sẽ trả về một string là đường dẫn của thư mục mà chúng ta đang làm việc trong đó.

import os
print["Path of current directory:", os.getcwd[]]
2. Lấy danh sách tập tin và thư mục con của một thư mục
Path of current directory: C:\python-examples

3. Tạo [create] một thư mục mới trong Python

from pathlib import Path

p = Path[__file__].resolve[]

with p.open[] as f: pass
with open[str[p]] as f: pass
with open[p] as f: pass

print['OK']
6 nằm trong thư mục
from pathlib import Path

p = Path[__file__].resolve[]

with p.open[] as f: pass
with open[str[p]] as f: pass
with open[p] as f: pass

print['OK']
7
.

1.2. Thay đổi current directory trong Python

2. Lấy danh sách tập tin và thư mục con của một thư mụcos hỗ trợ hàm

from pathlib import Path

p = Path[__file__].resolve[]

with p.open[] as f: pass
with open[str[p]] as f: pass
with open[p] as f: pass

print['OK']
8 để làm việc này.

import os
print["Path of current directory:", os.getcwd[]]
# change current directory
os.chdir['C:\Python\Python310']
print["Path of new current directory:", os.getcwd[]]
2. Lấy danh sách tập tin và thư mục con của một thư mục
from pathlib import Path

print[Path.cwd[]]  # /home/skovorodkin/stack
0

2. Lấy danh sách tập tin và thư mục con của một thư mục

3. Tạo [create] một thư mục mới trong Python

from pathlib import Path

p = Path[__file__].resolve[]

with p.open[] as f: pass
with open[str[p]] as f: pass
with open[p] as f: pass

print['OK']
9 giúp lấy danh sách tập tin và thư mục con của một thư mục được chỉ rõ đường dẫn. Nếu đường dẫn không được chỉ rõ thì
from pathlib import Path

p = Path[__file__].resolve[]

with p.open[] as f: pass
with open[str[p]] as f: pass
with open[p] as f: pass

print['OK']
9
sẽ lấy danh sách tập tin và thư mục con của current directory.

from pathlib import Path

print[Path.cwd[]]  # /home/skovorodkin/stack
12. Lấy danh sách tập tin và thư mục con của một thư mục
from pathlib import Path

print[Path.cwd[]]  # /home/skovorodkin/stack
2

3. Tạo [create] một thư mục mới trong Python

python3.5 scripts/2.py

Traceback [most recent call last]:
  File "scripts/2.py", line 11, in 
    with open[p] as f:
TypeError: invalid file: PosixPath['/home/skovorodkin/stack/scripts/2.py']
1. Còn đoạn code bên dưới sẽ list directories và files của current directory.

from pathlib import Path

print[Path.cwd[]]  # /home/skovorodkin/stack
32. Lấy danh sách tập tin và thư mục con của một thư mục
from pathlib import Path

print[Path.cwd[]]  # /home/skovorodkin/stack
4

3. Tạo [create] một thư mục mới trong Python

4. Đổi tên [rename] thư mục hoặc file trong Python

python3.5 scripts/2.py

Traceback [most recent call last]:
  File "scripts/2.py", line 11, in 
    with open[p] as f:
TypeError: invalid file: PosixPath['/home/skovorodkin/stack/scripts/2.py']
2 giúp tạo một thư mục mới với việc cung cấp đường dẫn đầy đủ [full path]. Nếu không cung cấp full path thì thư mục mới sẽ được tạo trong current directory.

from pathlib import Path

print[Path.cwd[]]  # /home/skovorodkin/stack
5

4. Đổi tên [rename] thư mục hoặc file trong Python

5. Xóa [delete] thư mục hoặc file trong Python

python3.5 scripts/2.py

Traceback [most recent call last]:
  File "scripts/2.py", line 11, in 
    with open[p] as f:
TypeError: invalid file: PosixPath['/home/skovorodkin/stack/scripts/2.py']
3. Hàm
python3.5 scripts/2.py

Traceback [most recent call last]:
  File "scripts/2.py", line 11, in 
    with open[p] as f:
TypeError: invalid file: PosixPath['/home/skovorodkin/stack/scripts/2.py']
3
có 2 tham số cơ bản. Tham số 1 là tên [hoặc đường dẫn] của folder hoặc file cũ. Tham số 2 là tên [hoặc đường dẫn] của folder hoặc file mới.

from pathlib import Path

print[Path.cwd[]]  # /home/skovorodkin/stack
62. Lấy danh sách tập tin và thư mục con của một thư mục
from pathlib import Path

print[Path.cwd[]]  # /home/skovorodkin/stack
7

5. Xóa [delete] thư mục hoặc file trong Python

In order to get the current working directory, use

print[Path[__file__].resolve[]]  # /home/skovorodkin/stack/scripts/1.py
5:
python3.5 scripts/2.py

Traceback [most recent call last]:
  File "scripts/2.py", line 11, in 
    with open[p] as f:
TypeError: invalid file: PosixPath['/home/skovorodkin/stack/scripts/2.py']
5
.

from pathlib import Path

print[Path.cwd[]]  # /home/skovorodkin/stack
82. Lấy danh sách tập tin và thư mục con của một thư mục
from pathlib import Path

print[Path.cwd[]]  # /home/skovorodkin/stack
9

3. Tạo [create] một thư mục mới trong Python

python3.5 scripts/2.py

Traceback [most recent call last]:
  File "scripts/2.py", line 11, in 
    with open[p] as f:
TypeError: invalid file: PosixPath['/home/skovorodkin/stack/scripts/2.py']
5 chỉ có thể xóa những folder rỗng [empty folder].

4. Đổi tên [rename] thư mục hoặc file trong Pythonhocit có chứa 1 file data.txt. Lúc này, sử dụng hàm

python3.5 scripts/2.py

Traceback [most recent call last]:
  File "scripts/2.py", line 11, in 
    with open[p] as f:
TypeError: invalid file: PosixPath['/home/skovorodkin/stack/scripts/2.py']
5 để xóa thì chương trình sẽ báo lỗi.

print[Path[__file__].resolve[]]  # /home/skovorodkin/stack/scripts/1.py
02. Lấy danh sách tập tin và thư mục con của một thư mục
print[Path[__file__].resolve[]]  # /home/skovorodkin/stack/scripts/1.py
1

3. Tạo [create] một thư mục mới trong Python

python3.5 scripts/2.py

Traceback [most recent call last]:
  File "scripts/2.py", line 11, in 
    with open[p] as f:
TypeError: invalid file: PosixPath['/home/skovorodkin/stack/scripts/2.py']
8 trong module shutil.

print[Path[__file__].resolve[]]  # /home/skovorodkin/stack/scripts/1.py
22. Lấy danh sách tập tin và thư mục con của một thư mục
print[Path[__file__].resolve[]]  # /home/skovorodkin/stack/scripts/1.py
3
  • 3. Tạo [create] một thư mục mới trong Python
  • Toán tử số học và toán tử quan hệ trong C++
  • Chương trình tìm dãy số Fibonacci trong Java
  • Toán tử logic, toán tử trên bit và toán tử gán trong C++
  • Hàm uniqid[] trong PHP

Bài Viết Liên Quan

Chủ Đề