{"id":15352220,"url":"https://github.com/xd-deng/common-questions-in-python","last_synced_at":"2026-03-19T03:06:36.851Z","repository":{"id":95966372,"uuid":"43945141","full_name":"XD-DENG/Common-Questions-in-Python","owner":"XD-DENG","description":"A collection of common questions about Python I encountered, and corresponding solutions","archived":false,"fork":false,"pushed_at":"2015-10-09T14:19:21.000Z","size":136,"stargazers_count":3,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-10-17T05:16:34.689Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":null,"has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/XD-DENG.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-10-09T09:13:54.000Z","updated_at":"2022-10-04T05:57:35.000Z","dependencies_parsed_at":"2023-03-05T20:26:43.896Z","dependency_job_id":null,"html_url":"https://github.com/XD-DENG/Common-Questions-in-Python","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/XD-DENG/Common-Questions-in-Python","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/XD-DENG%2FCommon-Questions-in-Python","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/XD-DENG%2FCommon-Questions-in-Python/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/XD-DENG%2FCommon-Questions-in-Python/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/XD-DENG%2FCommon-Questions-in-Python/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/XD-DENG","download_url":"https://codeload.github.com/XD-DENG/Common-Questions-in-Python/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/XD-DENG%2FCommon-Questions-in-Python/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29612509,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-02-19T10:52:55.328Z","status":"ssl_error","status_checked_at":"2026-02-19T10:52:26.323Z","response_time":117,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.6:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-01T12:09:01.438Z","updated_at":"2026-02-19T12:02:20.029Z","avatar_url":"https://github.com/XD-DENG.png","language":null,"readme":"## Common-Questions-in--Python\nA collection of common questions about Python I encountered, and corresponding solutions\n\n### 1. How to get the current time in Python?\n```\n\u003e\u003e\u003e import datetime\n\u003e\u003e\u003e datetime.datetime.now()\ndatetime.datetime(2015, 10, 9, 17, 15, 30, 498779)\n\u003e\u003e\u003e test = datetime.datetime.now()\n\u003e\u003e\u003e test.date()\ndatetime.date(2015, 10, 9)\n\u003e\u003e\u003e test.time()\ndatetime.time(17, 15, 38, 72927)\n```\n\n### 2. How to get current directory and change the directory?\n\n```\n\u003e\u003e\u003e import os\n\u003e\u003e\u003e os.getcwd()\n'/Users/nus/Documents'\n\u003e\u003e\u003e os.chdir(\"/Users/nus\")\n\u003e\u003e\u003e os.getcwd()\n'/Users/nus'\n```\n\n### 3. How to get the current user name?\n\n```\n\u003e\u003e\u003e import pwd\n\u003e\u003e\u003e import os\n\u003e\u003e\u003e pwd.getpwuid(os.getuid())\npwd.struct_passwd(pw_name='nus', pw_passwd='********', pw_uid=501, pw_gid=20, pw_gecos='NUS', pw_dir='/Users/nus', pw_shell='/bin/bash')\n\u003e\u003e\u003e pwd.getpwuid(os.getuid())[5]\n'/Users/nus'\n\u003e\u003e\u003e pwd.getpwuid(os.getuid())[0]\n'nus'\n```\n\n```\n\u003e\u003e\u003e from getpass import getuser\n\u003e\u003e\u003e getuser()\n'nus'\n```\n\n\n### 4. How to get the path of the current script?\n\n```\n# Please note this only works for script, and does NOT work interactively.\nimport os\nprint os.path.realpath(__file__)\n```\n\n### 5. How to get the unique values (distinct values) in a list?\n\n```\n\u003e\u003e\u003e test = [1,2,3,2,1,4,5,4]\n\u003e\u003e\u003e test\n[1, 2, 3, 2, 1, 4, 5, 4]\n\n\u003e\u003e\u003e set(test)\nset([1, 2, 3, 4, 5])\n\u003e\u003e\u003e list(set(test))\n[1, 2, 3, 4, 5]\n```\n\n```\n\u003e\u003e\u003e test = [\"a\", \"b\", \"a\", \"c\"]\n\u003e\u003e\u003e list(set(test))\n['a', 'c', 'b']\n```\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fxd-deng%2Fcommon-questions-in-python","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fxd-deng%2Fcommon-questions-in-python","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fxd-deng%2Fcommon-questions-in-python/lists"}