본문 바로가기

Python_Matter/Solve

Python Learn the basics Quiz 4

반응형

Q>

1. 여행에서 찍은 사진을 C:\python\helloworld.jpg의 경로에 보관하고 있다.

2. 이 사진의 파일의 경로를 임의의 변수에 할당하고, 사진이 보관되어 있는 폴더의 위치와 파일이름, 확장자를 출력하는 프로그램을 작성하시오.

3. 단, 출력에 있어 rfind 함수와 문자열 슬라이스 기능만을 활용하시오.

4. 아래와 같이 출력하시오. 

C:\myphoto

helloworld

jpg



A1>

file_folder = 'C:\python\helloworld.jpg'

root = file_folder[:file_folder.rfind('\\')]
folder = file_folder[file_folder.rfind('h'):file_folder.rfind('.')]
extension = file_folder[file_folder.rfind('j'):]

print(root)
print(folder)
print(extension)

A2>

file_folder = 'C:\python\helloworld.jpg'
root = file_folder.rfind('\\')

dir = file_folder.rfind('.')
dir_path = file_folder[:dir]
print(dir_path)
file_name = file_folder[root+1:dir]
print(file_name)
ext_name = file_folder[dir+1:]
print(ext_name)


O>

C:\myphoto

helloworld

jpg


Process finished with exit code 0

반응형

'Python_Matter > Solve' 카테고리의 다른 글

Python Learn the basics Quiz 6  (0) 2019.03.16
Python Learn the basics Quiz 5  (0) 2019.03.16
Python Learn the basics Quiz 3  (0) 2019.03.16
Python Learn the basics Quiz 2  (0) 2019.03.16
Python Learn the basics Quiz 1  (0) 2019.03.16