-
Notifications
You must be signed in to change notification settings - Fork 1
/
Basic_moving
50 lines (40 loc) · 1.35 KB
/
Basic_moving
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
Make sure you are in the correct directory. You will not automatically start in your Desktop. You will have to `navigate` there.
Think about it like clicking through directories and windows.
You can do this is Python using the `OS` library.
Example 1:
Check working directory
>>> import os
>>> os.getcwd()
'/Users/andrewakhlaghi'
For windows
Example 2:
Get list of files and subdirectories in a directory
>>> os.listdir()
Did you get an error? Are you using python2 or python3?
python2
>>> dir=os.getcwd() # this declares a variable contains your current working directory
>>> os.listdir(dir)
python3
>>> os.listdir()
Example 3:
Change working directory via relative path #If the directory is in your working directory
>>> os.chdir('Desktop')
>>> os.getcwd()
'/Users/andrewakhlaghi/Desktop'
Example 4:
Change working directory via absolute path # if the directory is outside of your working directory
>>> os.chdir('/Users/andrewakhlaghi/Desktop/Programs')
>>> os.getcwd()
'/Users/andrewakhlaghi/Desktop/Programs'
Example 5:
If you want to move a files:
>>> import shutil
>>> shutil.move(SOURCE_FILE, DESTINATION_FILE) # you can also use the shutil.copy or os.rename commands
Example 6:
get names of all files in directory
>>> os.listdir()
Example 7:
print all names in a readable way
>>> import pprint
>>> pp=pprint.PrettyPrinter()
>>> pp.pprint(os.listdir())